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". |
| 295 | * Return FAIL and give an error if it defined. |
| 296 | */ |
| 297 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 298 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 299 | { |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 300 | int c = p[len]; |
| 301 | |
| 302 | p[len] = NUL; |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 303 | if (lookup_script(p, len, FALSE) == OK |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 304 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 305 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 306 | || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK)) |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 307 | || find_imported(p, len, cctx) != NULL |
| 308 | || find_func_even_dead(p, FALSE, cctx) != NULL) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 309 | { |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 310 | p[len] = c; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 311 | semsg(_(e_name_already_defined_str), p); |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 312 | return FAIL; |
| 313 | } |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 314 | p[len] = c; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 315 | return OK; |
| 316 | } |
| 317 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 319 | ///////////////////////////////////////////////////////////////////// |
| 320 | // Following generate_ functions expect the caller to call ga_grow(). |
| 321 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 322 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 323 | #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] | 324 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 325 | /* |
| 326 | * Generate an instruction without arguments. |
| 327 | * Returns a pointer to the new instruction, NULL if failed. |
| 328 | */ |
| 329 | static isn_T * |
| 330 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 331 | { |
| 332 | garray_T *instr = &cctx->ctx_instr; |
| 333 | isn_T *isn; |
| 334 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 335 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 336 | if (ga_grow(instr, 1) == FAIL) |
| 337 | return NULL; |
| 338 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 339 | isn->isn_type = isn_type; |
| 340 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 341 | ++instr->ga_len; |
| 342 | |
| 343 | return isn; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Generate an instruction without arguments. |
| 348 | * "drop" will be removed from the stack. |
| 349 | * Returns a pointer to the new instruction, NULL if failed. |
| 350 | */ |
| 351 | static isn_T * |
| 352 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 353 | { |
| 354 | garray_T *stack = &cctx->ctx_type_stack; |
| 355 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 356 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 357 | stack->ga_len -= drop; |
| 358 | return generate_instr(cctx, isn_type); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 363 | */ |
| 364 | static isn_T * |
| 365 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 366 | { |
| 367 | isn_T *isn; |
| 368 | garray_T *stack = &cctx->ctx_type_stack; |
| 369 | |
| 370 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 371 | return NULL; |
| 372 | |
| 373 | if (ga_grow(stack, 1) == FAIL) |
| 374 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 375 | ((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] | 376 | ++stack->ga_len; |
| 377 | |
| 378 | return isn; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * 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] | 383 | * But only for simple types. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | */ |
| 385 | static int |
| 386 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 387 | { |
| 388 | isn_T *isn; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 389 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 390 | garray_T *stack = &cctx->ctx_type_stack; |
| 391 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 392 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 393 | switch ((*type)->tt_type) |
| 394 | { |
| 395 | // nothing to be done |
| 396 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 397 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 398 | // conversion possible |
| 399 | case VAR_SPECIAL: |
| 400 | case VAR_BOOL: |
| 401 | case VAR_NUMBER: |
| 402 | case VAR_FLOAT: |
| 403 | break; |
| 404 | |
| 405 | // conversion possible (with runtime check) |
| 406 | case VAR_ANY: |
| 407 | case VAR_UNKNOWN: |
| 408 | isntype = ISN_2STRING_ANY; |
| 409 | break; |
| 410 | |
| 411 | // conversion not possible |
| 412 | case VAR_VOID: |
| 413 | case VAR_BLOB: |
| 414 | case VAR_FUNC: |
| 415 | case VAR_PARTIAL: |
| 416 | case VAR_LIST: |
| 417 | case VAR_DICT: |
| 418 | case VAR_JOB: |
| 419 | case VAR_CHANNEL: |
| 420 | to_string_error((*type)->tt_type); |
| 421 | return FAIL; |
| 422 | } |
| 423 | |
| 424 | *type = &t_string; |
| 425 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 426 | return FAIL; |
| 427 | isn->isn_arg.number = offset; |
| 428 | |
| 429 | return OK; |
| 430 | } |
| 431 | |
| 432 | static int |
| 433 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 434 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 435 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 436 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 437 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 438 | { |
| 439 | if (*op == '+') |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 440 | emsg(_(e_wrong_argument_type_for_plus)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 441 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 442 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 443 | return FAIL; |
| 444 | } |
| 445 | return OK; |
| 446 | } |
| 447 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 448 | static int |
| 449 | generate_add_instr( |
| 450 | cctx_T *cctx, |
| 451 | vartype_T vartype, |
| 452 | type_T *type1, |
| 453 | type_T *type2) |
| 454 | { |
| 455 | isn_T *isn = generate_instr_drop(cctx, |
| 456 | vartype == VAR_NUMBER ? ISN_OPNR |
| 457 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 458 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 459 | #ifdef FEAT_FLOAT |
| 460 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 461 | #endif |
| 462 | : ISN_OPANY, 1); |
| 463 | |
| 464 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 465 | && type1->tt_type != VAR_ANY |
| 466 | && type2->tt_type != VAR_ANY |
| 467 | && check_number_or_float( |
| 468 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 469 | return FAIL; |
| 470 | |
| 471 | if (isn != NULL) |
| 472 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 473 | return isn == NULL ? FAIL : OK; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Get the type to use for an instruction for an operation on "type1" and |
| 478 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 479 | * fall back to runtime type checking. |
| 480 | */ |
| 481 | static vartype_T |
| 482 | operator_type(type_T *type1, type_T *type2) |
| 483 | { |
| 484 | if (type1->tt_type == type2->tt_type |
| 485 | && (type1->tt_type == VAR_NUMBER |
| 486 | || type1->tt_type == VAR_LIST |
| 487 | #ifdef FEAT_FLOAT |
| 488 | || type1->tt_type == VAR_FLOAT |
| 489 | #endif |
| 490 | || type1->tt_type == VAR_BLOB)) |
| 491 | return type1->tt_type; |
| 492 | return VAR_ANY; |
| 493 | } |
| 494 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 495 | /* |
| 496 | * Generate an instruction with two arguments. The instruction depends on the |
| 497 | * type of the arguments. |
| 498 | */ |
| 499 | static int |
| 500 | generate_two_op(cctx_T *cctx, char_u *op) |
| 501 | { |
| 502 | garray_T *stack = &cctx->ctx_type_stack; |
| 503 | type_T *type1; |
| 504 | type_T *type2; |
| 505 | vartype_T vartype; |
| 506 | isn_T *isn; |
| 507 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 508 | RETURN_OK_IF_SKIP(cctx); |
| 509 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 510 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 511 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 512 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 513 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 514 | |
| 515 | switch (*op) |
| 516 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 517 | case '+': |
| 518 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 519 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 520 | break; |
| 521 | |
| 522 | case '-': |
| 523 | case '*': |
| 524 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 525 | op) == FAIL) |
| 526 | return FAIL; |
| 527 | if (vartype == VAR_NUMBER) |
| 528 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 529 | #ifdef FEAT_FLOAT |
| 530 | else if (vartype == VAR_FLOAT) |
| 531 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 532 | #endif |
| 533 | else |
| 534 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 535 | if (isn != NULL) |
| 536 | isn->isn_arg.op.op_type = *op == '*' |
| 537 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 538 | break; |
| 539 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 540 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 541 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 542 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 543 | && type2->tt_type != VAR_NUMBER)) |
| 544 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 545 | emsg(_(e_percent_requires_number_arguments)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 546 | return FAIL; |
| 547 | } |
| 548 | isn = generate_instr_drop(cctx, |
| 549 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 550 | if (isn != NULL) |
| 551 | isn->isn_arg.op.op_type = EXPR_REM; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 556 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 557 | { |
| 558 | type_T *type = &t_any; |
| 559 | |
| 560 | #ifdef FEAT_FLOAT |
| 561 | // float+number and number+float results in float |
| 562 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 563 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 564 | type = &t_float; |
| 565 | #endif |
| 566 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 567 | } |
| 568 | |
| 569 | return OK; |
| 570 | } |
| 571 | |
| 572 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 573 | * Get the instruction to use for comparing "type1" with "type2" |
| 574 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 575 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 576 | static isntype_T |
| 577 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 578 | { |
| 579 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 580 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 581 | if (type1 == VAR_UNKNOWN) |
| 582 | type1 = VAR_ANY; |
| 583 | if (type2 == VAR_UNKNOWN) |
| 584 | type2 = VAR_ANY; |
| 585 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 586 | if (type1 == type2) |
| 587 | { |
| 588 | switch (type1) |
| 589 | { |
| 590 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 591 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 592 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 593 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 594 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 595 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 596 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 597 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 598 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 599 | default: isntype = ISN_COMPAREANY; break; |
| 600 | } |
| 601 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 602 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 603 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 604 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 605 | isntype = ISN_COMPAREANY; |
| 606 | |
| 607 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 608 | && (isntype == ISN_COMPAREBOOL |
| 609 | || isntype == ISN_COMPARESPECIAL |
| 610 | || isntype == ISN_COMPARENR |
| 611 | || isntype == ISN_COMPAREFLOAT)) |
| 612 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 613 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 614 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 615 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 616 | } |
| 617 | if (isntype == ISN_DROP |
| 618 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 619 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 620 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 621 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 622 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 623 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 624 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 625 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 626 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 627 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 628 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 629 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 630 | return isntype; |
| 631 | } |
| 632 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 633 | int |
| 634 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 635 | { |
| 636 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 637 | return FAIL; |
| 638 | return OK; |
| 639 | } |
| 640 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 643 | */ |
| 644 | static int |
| 645 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 646 | { |
| 647 | isntype_T isntype; |
| 648 | isn_T *isn; |
| 649 | garray_T *stack = &cctx->ctx_type_stack; |
| 650 | vartype_T type1; |
| 651 | vartype_T type2; |
| 652 | |
| 653 | RETURN_OK_IF_SKIP(cctx); |
| 654 | |
| 655 | // Get the known type of the two items on the stack. If they are matching |
| 656 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 657 | // checking. |
| 658 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 659 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 660 | isntype = get_compare_isn(exptype, type1, type2); |
| 661 | if (isntype == ISN_DROP) |
| 662 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 663 | |
| 664 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 665 | return FAIL; |
| 666 | isn->isn_arg.op.op_type = exptype; |
| 667 | isn->isn_arg.op.op_ic = ic; |
| 668 | |
| 669 | // takes two arguments, puts one bool back |
| 670 | if (stack->ga_len >= 2) |
| 671 | { |
| 672 | --stack->ga_len; |
| 673 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 674 | } |
| 675 | |
| 676 | return OK; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Generate an ISN_2BOOL instruction. |
| 681 | */ |
| 682 | static int |
| 683 | generate_2BOOL(cctx_T *cctx, int invert) |
| 684 | { |
| 685 | isn_T *isn; |
| 686 | garray_T *stack = &cctx->ctx_type_stack; |
| 687 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 688 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 689 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 690 | return FAIL; |
| 691 | isn->isn_arg.number = invert; |
| 692 | |
| 693 | // type becomes bool |
| 694 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 695 | |
| 696 | return OK; |
| 697 | } |
| 698 | |
| 699 | static int |
| 700 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 701 | { |
| 702 | isn_T *isn; |
| 703 | garray_T *stack = &cctx->ctx_type_stack; |
| 704 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 705 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 706 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 707 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 708 | // TODO: whole type, e.g. for a function also arg and return types |
| 709 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 710 | isn->isn_arg.type.ct_off = offset; |
| 711 | |
| 712 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 713 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 714 | |
| 715 | return OK; |
| 716 | } |
| 717 | |
| 718 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 719 | * Check that |
| 720 | * - "actual" is "expected" type or |
| 721 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 722 | * - return FAIL. |
| 723 | */ |
| 724 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 725 | need_type( |
| 726 | type_T *actual, |
| 727 | type_T *expected, |
| 728 | int offset, |
| 729 | cctx_T *cctx, |
| 730 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 731 | { |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 732 | if (expected == &t_bool && actual != &t_bool |
| 733 | && (actual->tt_flags & TTFLAG_BOOL_OK)) |
| 734 | { |
| 735 | // Using "0", "1" or the result of an expression with "&&" or "||" as a |
| 736 | // boolean is OK but requires a conversion. |
| 737 | generate_2BOOL(cctx, FALSE); |
| 738 | return OK; |
| 739 | } |
| 740 | |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 741 | if (check_type(expected, actual, FALSE, 0) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 742 | return OK; |
| 743 | if (actual->tt_type != VAR_ANY |
| 744 | && actual->tt_type != VAR_UNKNOWN |
| 745 | && !(actual->tt_type == VAR_FUNC |
| 746 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 747 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 748 | if (!silent) |
| 749 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 750 | return FAIL; |
| 751 | } |
| 752 | generate_TYPECHECK(cctx, expected, offset); |
| 753 | return OK; |
| 754 | } |
| 755 | |
| 756 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 757 | * Generate an ISN_PUSHNR instruction. |
| 758 | */ |
| 759 | static int |
| 760 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 761 | { |
| 762 | isn_T *isn; |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 763 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 764 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 765 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 766 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 767 | return FAIL; |
| 768 | isn->isn_arg.number = number; |
| 769 | |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 770 | if (number == 0 || number == 1) |
| 771 | { |
| 772 | type_T *type = alloc_type(cctx->ctx_type_list); |
| 773 | |
| 774 | // A 0 or 1 number can also be used as a bool. |
| 775 | if (type != NULL) |
| 776 | { |
| 777 | type->tt_type = VAR_NUMBER; |
| 778 | type->tt_flags = TTFLAG_BOOL_OK; |
| 779 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 780 | } |
| 781 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 782 | return OK; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Generate an ISN_PUSHBOOL instruction. |
| 787 | */ |
| 788 | static int |
| 789 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 790 | { |
| 791 | isn_T *isn; |
| 792 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 793 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 794 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 795 | return FAIL; |
| 796 | isn->isn_arg.number = number; |
| 797 | |
| 798 | return OK; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Generate an ISN_PUSHSPEC instruction. |
| 803 | */ |
| 804 | static int |
| 805 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 806 | { |
| 807 | isn_T *isn; |
| 808 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 809 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 810 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 811 | return FAIL; |
| 812 | isn->isn_arg.number = number; |
| 813 | |
| 814 | return OK; |
| 815 | } |
| 816 | |
| 817 | #ifdef FEAT_FLOAT |
| 818 | /* |
| 819 | * Generate an ISN_PUSHF instruction. |
| 820 | */ |
| 821 | static int |
| 822 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 823 | { |
| 824 | isn_T *isn; |
| 825 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 826 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 827 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 828 | return FAIL; |
| 829 | isn->isn_arg.fnumber = fnumber; |
| 830 | |
| 831 | return OK; |
| 832 | } |
| 833 | #endif |
| 834 | |
| 835 | /* |
| 836 | * Generate an ISN_PUSHS instruction. |
| 837 | * Consumes "str". |
| 838 | */ |
| 839 | static int |
| 840 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 841 | { |
| 842 | isn_T *isn; |
| 843 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 844 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 845 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 846 | return FAIL; |
| 847 | isn->isn_arg.string = str; |
| 848 | |
| 849 | return OK; |
| 850 | } |
| 851 | |
| 852 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 853 | * Generate an ISN_PUSHCHANNEL instruction. |
| 854 | * Consumes "channel". |
| 855 | */ |
| 856 | static int |
| 857 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 858 | { |
| 859 | isn_T *isn; |
| 860 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 861 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 862 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 863 | return FAIL; |
| 864 | isn->isn_arg.channel = channel; |
| 865 | |
| 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Generate an ISN_PUSHJOB instruction. |
| 871 | * Consumes "job". |
| 872 | */ |
| 873 | static int |
| 874 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 875 | { |
| 876 | isn_T *isn; |
| 877 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 878 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 879 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 880 | return FAIL; |
| 881 | isn->isn_arg.job = job; |
| 882 | |
| 883 | return OK; |
| 884 | } |
| 885 | |
| 886 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 887 | * Generate an ISN_PUSHBLOB instruction. |
| 888 | * Consumes "blob". |
| 889 | */ |
| 890 | static int |
| 891 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 892 | { |
| 893 | isn_T *isn; |
| 894 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 895 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 896 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 897 | return FAIL; |
| 898 | isn->isn_arg.blob = blob; |
| 899 | |
| 900 | return OK; |
| 901 | } |
| 902 | |
| 903 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 904 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 905 | * Consumes "name". |
| 906 | */ |
| 907 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 908 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 909 | { |
| 910 | isn_T *isn; |
| 911 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 912 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 913 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 914 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 915 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 916 | |
| 917 | return OK; |
| 918 | } |
| 919 | |
| 920 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 921 | * Generate an ISN_GETITEM instruction with "index". |
| 922 | */ |
| 923 | static int |
| 924 | generate_GETITEM(cctx_T *cctx, int index) |
| 925 | { |
| 926 | isn_T *isn; |
| 927 | garray_T *stack = &cctx->ctx_type_stack; |
| 928 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 929 | type_T *item_type = &t_any; |
| 930 | |
| 931 | RETURN_OK_IF_SKIP(cctx); |
| 932 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 933 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 934 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 935 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 936 | emsg(_(e_listreq)); |
| 937 | return FAIL; |
| 938 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 939 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 940 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 941 | return FAIL; |
| 942 | isn->isn_arg.number = index; |
| 943 | |
| 944 | // add the item type to the type stack |
| 945 | if (ga_grow(stack, 1) == FAIL) |
| 946 | return FAIL; |
| 947 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 948 | ++stack->ga_len; |
| 949 | return OK; |
| 950 | } |
| 951 | |
| 952 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 953 | * Generate an ISN_SLICE instruction with "count". |
| 954 | */ |
| 955 | static int |
| 956 | generate_SLICE(cctx_T *cctx, int count) |
| 957 | { |
| 958 | isn_T *isn; |
| 959 | |
| 960 | RETURN_OK_IF_SKIP(cctx); |
| 961 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 962 | return FAIL; |
| 963 | isn->isn_arg.number = count; |
| 964 | return OK; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 969 | */ |
| 970 | static int |
| 971 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 972 | { |
| 973 | isn_T *isn; |
| 974 | |
| 975 | RETURN_OK_IF_SKIP(cctx); |
| 976 | |
| 977 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 978 | return FAIL; |
| 979 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 980 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 981 | |
| 982 | return OK; |
| 983 | } |
| 984 | |
| 985 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 986 | * Generate an ISN_STORE instruction. |
| 987 | */ |
| 988 | static int |
| 989 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 990 | { |
| 991 | isn_T *isn; |
| 992 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 993 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 994 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 995 | return FAIL; |
| 996 | if (name != NULL) |
| 997 | isn->isn_arg.string = vim_strsave(name); |
| 998 | else |
| 999 | isn->isn_arg.number = idx; |
| 1000 | |
| 1001 | return OK; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1006 | */ |
| 1007 | static int |
| 1008 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1009 | { |
| 1010 | isn_T *isn; |
| 1011 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1012 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1013 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1014 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1015 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1016 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1017 | |
| 1018 | return OK; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * Generate an ISN_STOREOPT instruction |
| 1023 | */ |
| 1024 | static int |
| 1025 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1026 | { |
| 1027 | isn_T *isn; |
| 1028 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1029 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1030 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1031 | return FAIL; |
| 1032 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1033 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1034 | |
| 1035 | return OK; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * Generate an ISN_LOAD or similar instruction. |
| 1040 | */ |
| 1041 | static int |
| 1042 | generate_LOAD( |
| 1043 | cctx_T *cctx, |
| 1044 | isntype_T isn_type, |
| 1045 | int idx, |
| 1046 | char_u *name, |
| 1047 | type_T *type) |
| 1048 | { |
| 1049 | isn_T *isn; |
| 1050 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1051 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1052 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1053 | return FAIL; |
| 1054 | if (name != NULL) |
| 1055 | isn->isn_arg.string = vim_strsave(name); |
| 1056 | else |
| 1057 | isn->isn_arg.number = idx; |
| 1058 | |
| 1059 | return OK; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1063 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1064 | */ |
| 1065 | static int |
| 1066 | generate_LOADV( |
| 1067 | cctx_T *cctx, |
| 1068 | char_u *name, |
| 1069 | int error) |
| 1070 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1071 | int di_flags; |
| 1072 | int vidx = find_vim_var(name, &di_flags); |
| 1073 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1074 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1075 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1076 | if (vidx < 0) |
| 1077 | { |
| 1078 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1079 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1080 | return FAIL; |
| 1081 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1082 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1083 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1084 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1088 | * Generate an ISN_UNLET instruction. |
| 1089 | */ |
| 1090 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1091 | 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] | 1092 | { |
| 1093 | isn_T *isn; |
| 1094 | |
| 1095 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1096 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1097 | return FAIL; |
| 1098 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1099 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1100 | |
| 1101 | return OK; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1105 | * Generate an ISN_LOADS instruction. |
| 1106 | */ |
| 1107 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1108 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1109 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1110 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1111 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1112 | int sid, |
| 1113 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1114 | { |
| 1115 | isn_T *isn; |
| 1116 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1117 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1118 | if (isn_type == ISN_LOADS) |
| 1119 | isn = generate_instr_type(cctx, isn_type, type); |
| 1120 | else |
| 1121 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1122 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1123 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1124 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1125 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1126 | |
| 1127 | return OK; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1132 | */ |
| 1133 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1134 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1135 | cctx_T *cctx, |
| 1136 | isntype_T isn_type, |
| 1137 | int sid, |
| 1138 | int idx, |
| 1139 | type_T *type) |
| 1140 | { |
| 1141 | isn_T *isn; |
| 1142 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1143 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1144 | if (isn_type == ISN_LOADSCRIPT) |
| 1145 | isn = generate_instr_type(cctx, isn_type, type); |
| 1146 | else |
| 1147 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1148 | if (isn == NULL) |
| 1149 | return FAIL; |
| 1150 | isn->isn_arg.script.script_sid = sid; |
| 1151 | isn->isn_arg.script.script_idx = idx; |
| 1152 | return OK; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * Generate an ISN_NEWLIST instruction. |
| 1157 | */ |
| 1158 | static int |
| 1159 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1160 | { |
| 1161 | isn_T *isn; |
| 1162 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1163 | type_T *type; |
| 1164 | type_T *member; |
| 1165 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1166 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1167 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1168 | return FAIL; |
| 1169 | isn->isn_arg.number = count; |
| 1170 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1171 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1172 | if (count == 0) |
| 1173 | member = &t_void; |
| 1174 | else |
| 1175 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1176 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1177 | cctx->ctx_type_list); |
| 1178 | type = get_list_type(member, cctx->ctx_type_list); |
| 1179 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1180 | // drop the value types |
| 1181 | stack->ga_len -= count; |
| 1182 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1183 | // add the list type to the type stack |
| 1184 | if (ga_grow(stack, 1) == FAIL) |
| 1185 | return FAIL; |
| 1186 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1187 | ++stack->ga_len; |
| 1188 | |
| 1189 | return OK; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Generate an ISN_NEWDICT instruction. |
| 1194 | */ |
| 1195 | static int |
| 1196 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1197 | { |
| 1198 | isn_T *isn; |
| 1199 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1200 | type_T *type; |
| 1201 | type_T *member; |
| 1202 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1203 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1204 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1205 | return FAIL; |
| 1206 | isn->isn_arg.number = count; |
| 1207 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1208 | if (count == 0) |
| 1209 | member = &t_void; |
| 1210 | else |
| 1211 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1212 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1213 | cctx->ctx_type_list); |
| 1214 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1215 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1216 | // drop the key and value types |
| 1217 | stack->ga_len -= 2 * count; |
| 1218 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1219 | // add the dict type to the type stack |
| 1220 | if (ga_grow(stack, 1) == FAIL) |
| 1221 | return FAIL; |
| 1222 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1223 | ++stack->ga_len; |
| 1224 | |
| 1225 | return OK; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * Generate an ISN_FUNCREF instruction. |
| 1230 | */ |
| 1231 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1232 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1233 | { |
| 1234 | isn_T *isn; |
| 1235 | garray_T *stack = &cctx->ctx_type_stack; |
| 1236 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1237 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1238 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1239 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1240 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1241 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1242 | |
| 1243 | if (ga_grow(stack, 1) == FAIL) |
| 1244 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1245 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1246 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1247 | ++stack->ga_len; |
| 1248 | |
| 1249 | return OK; |
| 1250 | } |
| 1251 | |
| 1252 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1253 | * Generate an ISN_NEWFUNC instruction. |
| 1254 | */ |
| 1255 | static int |
| 1256 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1257 | { |
| 1258 | isn_T *isn; |
| 1259 | char_u *name; |
| 1260 | |
| 1261 | RETURN_OK_IF_SKIP(cctx); |
| 1262 | name = vim_strsave(lambda_name); |
| 1263 | if (name == NULL) |
| 1264 | return FAIL; |
| 1265 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1266 | return FAIL; |
| 1267 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1268 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1269 | |
| 1270 | return OK; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1274 | * Generate an ISN_JUMP instruction. |
| 1275 | */ |
| 1276 | static int |
| 1277 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1278 | { |
| 1279 | isn_T *isn; |
| 1280 | garray_T *stack = &cctx->ctx_type_stack; |
| 1281 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1282 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1283 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1284 | return FAIL; |
| 1285 | isn->isn_arg.jump.jump_when = when; |
| 1286 | isn->isn_arg.jump.jump_where = where; |
| 1287 | |
| 1288 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1289 | --stack->ga_len; |
| 1290 | |
| 1291 | return OK; |
| 1292 | } |
| 1293 | |
| 1294 | static int |
| 1295 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1296 | { |
| 1297 | isn_T *isn; |
| 1298 | garray_T *stack = &cctx->ctx_type_stack; |
| 1299 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1300 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1301 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1302 | return FAIL; |
| 1303 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1304 | |
| 1305 | if (ga_grow(stack, 1) == FAIL) |
| 1306 | return FAIL; |
| 1307 | // type doesn't matter, will be stored next |
| 1308 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1309 | ++stack->ga_len; |
| 1310 | |
| 1311 | return OK; |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1316 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1317 | * Return FAIL if the number of arguments is wrong. |
| 1318 | */ |
| 1319 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1320 | 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] | 1321 | { |
| 1322 | isn_T *isn; |
| 1323 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1324 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1325 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1326 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1327 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1328 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1329 | argoff = check_internal_func(func_idx, argcount); |
| 1330 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1331 | return FAIL; |
| 1332 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1333 | if (method_call && argoff > 1) |
| 1334 | { |
| 1335 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1336 | return FAIL; |
| 1337 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1338 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1339 | } |
| 1340 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1341 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1342 | return FAIL; |
| 1343 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1344 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1345 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1346 | for (i = 0; i < argcount; ++i) |
| 1347 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1348 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1349 | stack->ga_len -= argcount; // drop the arguments |
| 1350 | if (ga_grow(stack, 1) == FAIL) |
| 1351 | return FAIL; |
| 1352 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1353 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1354 | ++stack->ga_len; // add return value |
| 1355 | |
| 1356 | return OK; |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1361 | * Return FAIL if the number of arguments is wrong. |
| 1362 | */ |
| 1363 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1364 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1365 | { |
| 1366 | isn_T *isn; |
| 1367 | garray_T *stack = &cctx->ctx_type_stack; |
| 1368 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1369 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1370 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1371 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1372 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1373 | { |
| 1374 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1375 | return FAIL; |
| 1376 | } |
| 1377 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1378 | { |
| 1379 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1380 | return FAIL; |
| 1381 | } |
| 1382 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1383 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1384 | { |
| 1385 | int i; |
| 1386 | |
| 1387 | for (i = 0; i < argcount; ++i) |
| 1388 | { |
| 1389 | type_T *expected; |
| 1390 | type_T *actual; |
| 1391 | |
| 1392 | if (i < regular_args) |
| 1393 | { |
| 1394 | if (ufunc->uf_arg_types == NULL) |
| 1395 | continue; |
| 1396 | expected = ufunc->uf_arg_types[i]; |
| 1397 | } |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1398 | else if (ufunc->uf_va_type == NULL) |
| 1399 | // possibly a lambda |
| 1400 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1401 | else |
| 1402 | expected = ufunc->uf_va_type->tt_member; |
| 1403 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1404 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1405 | { |
| 1406 | arg_type_mismatch(expected, actual, i + 1); |
| 1407 | return FAIL; |
| 1408 | } |
| 1409 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1410 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1411 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1412 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1413 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1414 | } |
| 1415 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1416 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1417 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1418 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1419 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1420 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1421 | { |
| 1422 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1423 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1424 | } |
| 1425 | else |
| 1426 | { |
| 1427 | // A user function may be deleted and redefined later, can't use the |
| 1428 | // ufunc pointer, need to look it up again at runtime. |
| 1429 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1430 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1431 | } |
| 1432 | |
| 1433 | stack->ga_len -= argcount; // drop the arguments |
| 1434 | if (ga_grow(stack, 1) == FAIL) |
| 1435 | return FAIL; |
| 1436 | // add return value |
| 1437 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1438 | ++stack->ga_len; |
| 1439 | |
| 1440 | return OK; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1445 | */ |
| 1446 | static int |
| 1447 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1448 | { |
| 1449 | isn_T *isn; |
| 1450 | garray_T *stack = &cctx->ctx_type_stack; |
| 1451 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1452 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1453 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1454 | return FAIL; |
| 1455 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1456 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1457 | |
| 1458 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1459 | if (ga_grow(stack, 1) == FAIL) |
| 1460 | return FAIL; |
| 1461 | // add return value |
| 1462 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1463 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1464 | |
| 1465 | return OK; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1470 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1471 | */ |
| 1472 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1473 | generate_PCALL( |
| 1474 | cctx_T *cctx, |
| 1475 | int argcount, |
| 1476 | char_u *name, |
| 1477 | type_T *type, |
| 1478 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1479 | { |
| 1480 | isn_T *isn; |
| 1481 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1482 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1483 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1484 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1485 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1486 | if (type->tt_type == VAR_ANY) |
| 1487 | ret_type = &t_any; |
| 1488 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1489 | { |
| 1490 | if (type->tt_argcount != -1) |
| 1491 | { |
| 1492 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1493 | |
| 1494 | if (argcount < type->tt_min_argcount - varargs) |
| 1495 | { |
| 1496 | semsg(_(e_toofewarg), "[reference]"); |
| 1497 | return FAIL; |
| 1498 | } |
| 1499 | if (!varargs && argcount > type->tt_argcount) |
| 1500 | { |
| 1501 | semsg(_(e_toomanyarg), "[reference]"); |
| 1502 | return FAIL; |
| 1503 | } |
| 1504 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1505 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1506 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1507 | else |
| 1508 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1509 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1510 | return FAIL; |
| 1511 | } |
| 1512 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1513 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1514 | return FAIL; |
| 1515 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1516 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1517 | |
| 1518 | stack->ga_len -= argcount; // drop the arguments |
| 1519 | |
| 1520 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1521 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1522 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1523 | // If partial is above the arguments it must be cleared and replaced with |
| 1524 | // the return value. |
| 1525 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1526 | return FAIL; |
| 1527 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1528 | return OK; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1532 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1533 | */ |
| 1534 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1535 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1536 | { |
| 1537 | isn_T *isn; |
| 1538 | garray_T *stack = &cctx->ctx_type_stack; |
| 1539 | type_T *type; |
| 1540 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1541 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1542 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1543 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1544 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1546 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1547 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1548 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1549 | { |
| 1550 | emsg(_(e_dictreq)); |
| 1551 | return FAIL; |
| 1552 | } |
| 1553 | // change dict type to dict member type |
| 1554 | if (type->tt_type == VAR_DICT) |
| 1555 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1556 | |
| 1557 | return OK; |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * Generate an ISN_ECHO instruction. |
| 1562 | */ |
| 1563 | static int |
| 1564 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1565 | { |
| 1566 | isn_T *isn; |
| 1567 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1568 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1569 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1570 | return FAIL; |
| 1571 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1572 | isn->isn_arg.echo.echo_count = count; |
| 1573 | |
| 1574 | return OK; |
| 1575 | } |
| 1576 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1577 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1578 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1579 | */ |
| 1580 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1581 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1582 | { |
| 1583 | isn_T *isn; |
| 1584 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1585 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1586 | return FAIL; |
| 1587 | isn->isn_arg.number = count; |
| 1588 | |
| 1589 | return OK; |
| 1590 | } |
| 1591 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 1592 | /* |
| 1593 | * Generate an ISN_PUT instruction. |
| 1594 | */ |
| 1595 | static int |
| 1596 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1597 | { |
| 1598 | isn_T *isn; |
| 1599 | |
| 1600 | RETURN_OK_IF_SKIP(cctx); |
| 1601 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1602 | return FAIL; |
| 1603 | isn->isn_arg.put.put_regname = regname; |
| 1604 | isn->isn_arg.put.put_lnum = lnum; |
| 1605 | return OK; |
| 1606 | } |
| 1607 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1608 | static int |
| 1609 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1610 | { |
| 1611 | isn_T *isn; |
| 1612 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1613 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1614 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1615 | return FAIL; |
| 1616 | isn->isn_arg.string = vim_strsave(line); |
| 1617 | return OK; |
| 1618 | } |
| 1619 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1620 | static int |
| 1621 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1622 | { |
| 1623 | isn_T *isn; |
| 1624 | |
| 1625 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1626 | return FAIL; |
| 1627 | isn->isn_arg.number = count; |
| 1628 | return OK; |
| 1629 | } |
| 1630 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1631 | /* |
| 1632 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1633 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1634 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1635 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1636 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1637 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1638 | lvar_T *lvar; |
| 1639 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1640 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1641 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1642 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1643 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1647 | return NULL; |
| 1648 | 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] | 1649 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1650 | // Every local variable uses the next entry on the stack. We could re-use |
| 1651 | // the last ones when leaving a scope, but then variables used in a closure |
| 1652 | // might get overwritten. To keep things simple do not re-use stack |
| 1653 | // entries. This is less efficient, but memory is cheap these days. |
| 1654 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1655 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1656 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1657 | lvar->lv_const = isConst; |
| 1658 | lvar->lv_type = type; |
| 1659 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1660 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1664 | * Remove local variables above "new_top". |
| 1665 | */ |
| 1666 | static void |
| 1667 | unwind_locals(cctx_T *cctx, int new_top) |
| 1668 | { |
| 1669 | if (cctx->ctx_locals.ga_len > new_top) |
| 1670 | { |
| 1671 | int idx; |
| 1672 | lvar_T *lvar; |
| 1673 | |
| 1674 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1675 | { |
| 1676 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1677 | vim_free(lvar->lv_name); |
| 1678 | } |
| 1679 | } |
| 1680 | cctx->ctx_locals.ga_len = new_top; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Free all local variables. |
| 1685 | */ |
| 1686 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1687 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1688 | { |
| 1689 | unwind_locals(cctx, 0); |
| 1690 | ga_clear(&cctx->ctx_locals); |
| 1691 | } |
| 1692 | |
| 1693 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1694 | * Find "name" in script-local items of script "sid". |
| 1695 | * Returns the index in "sn_var_vals" if found. |
| 1696 | * If found but not in "sn_var_vals" returns -1. |
| 1697 | * If not found returns -2. |
| 1698 | */ |
| 1699 | int |
| 1700 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1701 | { |
| 1702 | hashtab_T *ht; |
| 1703 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1704 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1705 | int idx; |
| 1706 | |
| 1707 | // First look the name up in the hashtable. |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1708 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1709 | return -1; |
| 1710 | ht = &SCRIPT_VARS(sid); |
| 1711 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1712 | if (di == NULL) |
| 1713 | return -2; |
| 1714 | |
| 1715 | // Now find the svar_T index in sn_var_vals. |
| 1716 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1717 | { |
| 1718 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1719 | |
| 1720 | if (sv->sv_tv == &di->di_tv) |
| 1721 | { |
| 1722 | if (check_writable && sv->sv_const) |
| 1723 | semsg(_(e_readonlyvar), name); |
| 1724 | return idx; |
| 1725 | } |
| 1726 | } |
| 1727 | return -1; |
| 1728 | } |
| 1729 | |
| 1730 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1731 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1732 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1733 | */ |
| 1734 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1735 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1736 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1737 | int idx; |
| 1738 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1739 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1740 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1741 | if (cctx != NULL) |
| 1742 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1743 | { |
| 1744 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1745 | + idx; |
| 1746 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1747 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1748 | : STRLEN(import->imp_name) == len |
| 1749 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1750 | return import; |
| 1751 | } |
| 1752 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1753 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1754 | } |
| 1755 | |
| 1756 | imported_T * |
| 1757 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1758 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1759 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1760 | int idx; |
| 1761 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1762 | if (!SCRIPT_ID_VALID(sid)) |
| 1763 | return NULL; |
| 1764 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1765 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1766 | { |
| 1767 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1768 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1769 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1770 | : STRLEN(import->imp_name) == len |
| 1771 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1772 | return import; |
| 1773 | } |
| 1774 | return NULL; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1778 | * Free all imported variables. |
| 1779 | */ |
| 1780 | static void |
| 1781 | free_imported(cctx_T *cctx) |
| 1782 | { |
| 1783 | int idx; |
| 1784 | |
| 1785 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1786 | { |
| 1787 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1788 | |
| 1789 | vim_free(import->imp_name); |
| 1790 | } |
| 1791 | ga_clear(&cctx->ctx_imports); |
| 1792 | } |
| 1793 | |
| 1794 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1795 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1796 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1797 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1798 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1799 | { |
| 1800 | return p[0] == '#' && p[1] != '{'; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * Return a pointer to the next line that isn't empty or only contains a |
| 1805 | * comment. Skips over white space. |
| 1806 | * Returns NULL if there is none. |
| 1807 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1808 | char_u * |
| 1809 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1810 | { |
| 1811 | int lnum = cctx->ctx_lnum; |
| 1812 | |
| 1813 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1814 | { |
| 1815 | 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] | 1816 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1817 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 1818 | // ignore NULLs inserted for continuation lines |
| 1819 | if (line != NULL) |
| 1820 | { |
| 1821 | p = skipwhite(line); |
| 1822 | if (*p != NUL && !vim9_comment_start(p)) |
| 1823 | return p; |
| 1824 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1825 | } |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1830 | * Called when checking for a following operator at "arg". When the rest of |
| 1831 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1832 | * line return a pointer to it and set "nextp". |
| 1833 | * Otherwise skip over white space. |
| 1834 | */ |
| 1835 | static char_u * |
| 1836 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1837 | { |
| 1838 | char_u *p = skipwhite(arg); |
| 1839 | |
| 1840 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1841 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1842 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1843 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1844 | if (*nextp != NULL) |
| 1845 | return *nextp; |
| 1846 | } |
| 1847 | return p; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1851 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1852 | * 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] | 1853 | * Returns NULL when at the end. |
| 1854 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1855 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1856 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1857 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1858 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1859 | |
| 1860 | do |
| 1861 | { |
| 1862 | ++cctx->ctx_lnum; |
| 1863 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1864 | { |
| 1865 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1866 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1867 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1868 | 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] | 1869 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1870 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1871 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1872 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1873 | return line; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1877 | * 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] | 1878 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1879 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1880 | */ |
| 1881 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1882 | 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] | 1883 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1884 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1885 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1886 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1887 | |
| 1888 | if (next == NULL) |
| 1889 | return FAIL; |
| 1890 | *arg = skipwhite(next); |
| 1891 | } |
| 1892 | return OK; |
| 1893 | } |
| 1894 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1895 | /* |
| 1896 | * Idem, and give an error when failed. |
| 1897 | */ |
| 1898 | static int |
| 1899 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1900 | { |
| 1901 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1902 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1903 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1904 | return FAIL; |
| 1905 | } |
| 1906 | return OK; |
| 1907 | } |
| 1908 | |
| 1909 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1910 | // Structure passed between the compile_expr* functions to keep track of |
| 1911 | // constants that have been parsed but for which no code was produced yet. If |
| 1912 | // possible expressions on these constants are applied at compile time. If |
| 1913 | // that is not possible, the code to push the constants needs to be generated |
| 1914 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1915 | // Using 50 should be more than enough of 5 levels of (). |
| 1916 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1917 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1918 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1919 | int pp_used; // active entries in pp_tv[] |
| 1920 | } ppconst_T; |
| 1921 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1922 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1923 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1924 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1925 | /* |
| 1926 | * Generate a PUSH instruction for "tv". |
| 1927 | * "tv" will be consumed or cleared. |
| 1928 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1929 | */ |
| 1930 | static int |
| 1931 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1932 | { |
| 1933 | if (tv != NULL) |
| 1934 | { |
| 1935 | switch (tv->v_type) |
| 1936 | { |
| 1937 | case VAR_UNKNOWN: |
| 1938 | break; |
| 1939 | case VAR_BOOL: |
| 1940 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1941 | break; |
| 1942 | case VAR_SPECIAL: |
| 1943 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1944 | break; |
| 1945 | case VAR_NUMBER: |
| 1946 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1947 | break; |
| 1948 | #ifdef FEAT_FLOAT |
| 1949 | case VAR_FLOAT: |
| 1950 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1951 | break; |
| 1952 | #endif |
| 1953 | case VAR_BLOB: |
| 1954 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1955 | tv->vval.v_blob = NULL; |
| 1956 | break; |
| 1957 | case VAR_STRING: |
| 1958 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1959 | tv->vval.v_string = NULL; |
| 1960 | break; |
| 1961 | default: |
| 1962 | iemsg("constant type not supported"); |
| 1963 | clear_tv(tv); |
| 1964 | return FAIL; |
| 1965 | } |
| 1966 | tv->v_type = VAR_UNKNOWN; |
| 1967 | } |
| 1968 | return OK; |
| 1969 | } |
| 1970 | |
| 1971 | /* |
| 1972 | * Generate code for any ppconst entries. |
| 1973 | */ |
| 1974 | static int |
| 1975 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1976 | { |
| 1977 | int i; |
| 1978 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1979 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1980 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1981 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1982 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1983 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1984 | ret = FAIL; |
| 1985 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1986 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1987 | return ret; |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * Clear ppconst constants. Used when failing. |
| 1992 | */ |
| 1993 | static void |
| 1994 | clear_ppconst(ppconst_T *ppconst) |
| 1995 | { |
| 1996 | int i; |
| 1997 | |
| 1998 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1999 | clear_tv(&ppconst->pp_tv[i]); |
| 2000 | ppconst->pp_used = 0; |
| 2001 | } |
| 2002 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2003 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2004 | * Generate an instruction to load script-local variable "name", without the |
| 2005 | * leading "s:". |
| 2006 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2007 | */ |
| 2008 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2009 | compile_load_scriptvar( |
| 2010 | cctx_T *cctx, |
| 2011 | char_u *name, // variable NUL terminated |
| 2012 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2013 | char_u **end, // end of variable |
| 2014 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2015 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2016 | scriptitem_T *si; |
| 2017 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2018 | imported_T *import; |
| 2019 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2020 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2021 | return FAIL; |
| 2022 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 2023 | idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2024 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2025 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2026 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2027 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2028 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2029 | } |
| 2030 | if (idx >= 0) |
| 2031 | { |
| 2032 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2033 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2034 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2035 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2036 | return OK; |
| 2037 | } |
| 2038 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2039 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2040 | if (import != NULL) |
| 2041 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2042 | if (import->imp_all) |
| 2043 | { |
| 2044 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2045 | char_u *exp_name; |
| 2046 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2047 | ufunc_T *ufunc; |
| 2048 | type_T *type; |
| 2049 | |
| 2050 | // Used "import * as Name", need to lookup the member. |
| 2051 | if (*p != '.') |
| 2052 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2053 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2054 | return FAIL; |
| 2055 | } |
| 2056 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2057 | if (VIM_ISWHITE(*p)) |
| 2058 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2059 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2060 | return FAIL; |
| 2061 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2062 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2063 | // isolate one name |
| 2064 | exp_name = p; |
| 2065 | while (eval_isnamec(*p)) |
| 2066 | ++p; |
| 2067 | cc = *p; |
| 2068 | *p = NUL; |
| 2069 | |
| 2070 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2071 | *p = cc; |
| 2072 | p = skipwhite(p); |
| 2073 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2074 | // TODO: what if it is a function? |
| 2075 | if (idx < 0) |
| 2076 | return FAIL; |
| 2077 | *end = p; |
| 2078 | |
| 2079 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2080 | import->imp_sid, |
| 2081 | idx, |
| 2082 | type); |
| 2083 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2084 | else if (import->imp_funcname != NULL) |
| 2085 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2086 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2087 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2088 | import->imp_sid, |
| 2089 | import->imp_var_vals_idx, |
| 2090 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2091 | return OK; |
| 2092 | } |
| 2093 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2094 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2095 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2096 | return FAIL; |
| 2097 | } |
| 2098 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2099 | static int |
| 2100 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2101 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2102 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2103 | |
| 2104 | if (ufunc == NULL) |
| 2105 | return FAIL; |
| 2106 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2107 | // Need to compile any default values to get the argument types. |
| 2108 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2109 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2110 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2111 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2114 | /* |
| 2115 | * Compile a variable name into a load instruction. |
| 2116 | * "end" points to just after the name. |
| 2117 | * When "error" is FALSE do not give an error when not found. |
| 2118 | */ |
| 2119 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2120 | compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2121 | { |
| 2122 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2123 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2124 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2125 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2126 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2127 | |
| 2128 | if (*(*arg + 1) == ':') |
| 2129 | { |
| 2130 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2131 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2132 | { |
| 2133 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2134 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2135 | switch (**arg) |
| 2136 | { |
| 2137 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2138 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2139 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2140 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2141 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2142 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2143 | goto theend; |
| 2144 | } |
| 2145 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2146 | goto theend; |
| 2147 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2148 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2149 | else |
| 2150 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2151 | isntype_T isn_type = ISN_DROP; |
| 2152 | |
| 2153 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2154 | if (name == NULL) |
| 2155 | return FAIL; |
| 2156 | |
| 2157 | switch (**arg) |
| 2158 | { |
| 2159 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2160 | break; |
| 2161 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2162 | NULL, NULL, error); |
| 2163 | break; |
| 2164 | case 'g': isn_type = ISN_LOADG; break; |
| 2165 | case 'w': isn_type = ISN_LOADW; break; |
| 2166 | case 't': isn_type = ISN_LOADT; break; |
| 2167 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2168 | default: semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2169 | goto theend; |
| 2170 | } |
| 2171 | if (isn_type != ISN_DROP) |
| 2172 | { |
| 2173 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2174 | // variables can be defined later, thus we don't check if it |
| 2175 | // exists, give error at runtime. |
| 2176 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2178 | } |
| 2179 | } |
| 2180 | else |
| 2181 | { |
| 2182 | size_t len = end - *arg; |
| 2183 | int idx; |
| 2184 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2185 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2186 | |
| 2187 | name = vim_strnsave(*arg, end - *arg); |
| 2188 | if (name == NULL) |
| 2189 | return FAIL; |
| 2190 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2191 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2192 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2193 | if (!gen_load_outer) |
| 2194 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2195 | } |
| 2196 | else |
| 2197 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2198 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2199 | |
| 2200 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2201 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2202 | type = lvar->lv_type; |
| 2203 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2204 | if (lvar->lv_from_outer) |
| 2205 | gen_load_outer = TRUE; |
| 2206 | else |
| 2207 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2208 | } |
| 2209 | else |
| 2210 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2211 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2212 | // already exists in a Vim9 script or when it's imported. |
| 2213 | if (lookup_script(*arg, len, TRUE) == OK |
| 2214 | || find_imported(name, 0, cctx) != NULL) |
| 2215 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2216 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2217 | // When the name starts with an uppercase letter or "x:" it |
| 2218 | // can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2219 | // TODO: this is just guessing |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2220 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2221 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | if (gen_load) |
| 2225 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2226 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2227 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2228 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2229 | cctx->ctx_outer_used = TRUE; |
| 2230 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | *arg = end; |
| 2234 | |
| 2235 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2236 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2237 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2238 | vim_free(name); |
| 2239 | return res; |
| 2240 | } |
| 2241 | |
| 2242 | /* |
| 2243 | * Compile the argument expressions. |
| 2244 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2245 | */ |
| 2246 | static int |
| 2247 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2248 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2249 | char_u *p = *arg; |
| 2250 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2251 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2252 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2253 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2254 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2255 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2256 | if (*p == ')') |
| 2257 | { |
| 2258 | *arg = p + 1; |
| 2259 | return OK; |
| 2260 | } |
| 2261 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2262 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2263 | return FAIL; |
| 2264 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2265 | |
| 2266 | if (*p != ',' && *skipwhite(p) == ',') |
| 2267 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2268 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2269 | p = skipwhite(p); |
| 2270 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2271 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2272 | { |
| 2273 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2274 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2275 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2276 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2277 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2278 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2279 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2280 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2281 | emsg(_(e_missing_close)); |
| 2282 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * Compile a function call: name(arg1, arg2) |
| 2287 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2288 | * "argcount_init" is 1 for "value->method()" |
| 2289 | * Instructions: |
| 2290 | * EVAL arg1 |
| 2291 | * EVAL arg2 |
| 2292 | * BCALL / DCALL / UCALL |
| 2293 | */ |
| 2294 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2295 | compile_call( |
| 2296 | char_u **arg, |
| 2297 | size_t varlen, |
| 2298 | cctx_T *cctx, |
| 2299 | ppconst_T *ppconst, |
| 2300 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2301 | { |
| 2302 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2303 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2304 | int argcount = argcount_init; |
| 2305 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2306 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2307 | char_u *tofree = NULL; |
| 2308 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2309 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2310 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2311 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2312 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2313 | // we can evaluate "has('name')" at compile time |
| 2314 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2315 | { |
| 2316 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2317 | typval_T argvars[2]; |
| 2318 | |
| 2319 | argvars[0].v_type = VAR_UNKNOWN; |
| 2320 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2321 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2322 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2323 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2324 | s = skipwhite(s); |
| 2325 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2326 | { |
| 2327 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2328 | |
| 2329 | *arg = s + 1; |
| 2330 | argvars[1].v_type = VAR_UNKNOWN; |
| 2331 | tv->v_type = VAR_NUMBER; |
| 2332 | tv->vval.v_number = 0; |
| 2333 | f_has(argvars, tv); |
| 2334 | clear_tv(&argvars[0]); |
| 2335 | ++ppconst->pp_used; |
| 2336 | return OK; |
| 2337 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2338 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2342 | return FAIL; |
| 2343 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2344 | if (varlen >= sizeof(namebuf)) |
| 2345 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2346 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2347 | return FAIL; |
| 2348 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2349 | vim_strncpy(namebuf, *arg, varlen); |
| 2350 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2351 | |
| 2352 | *arg = skipwhite(*arg + varlen + 1); |
| 2353 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2354 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2355 | |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2356 | is_autoload = vim_strchr(name, '#') != NULL; |
| 2357 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2358 | { |
| 2359 | int idx; |
| 2360 | |
| 2361 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2362 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2363 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2364 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2365 | else |
| 2366 | semsg(_(e_unknownfunc), namebuf); |
| 2367 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2370 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2371 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2372 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2373 | { |
| 2374 | res = generate_CALL(cctx, ufunc, argcount); |
| 2375 | goto theend; |
| 2376 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2377 | |
| 2378 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2379 | // 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] | 2380 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2381 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2382 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2383 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2384 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2385 | garray_T *stack = &cctx->ctx_type_stack; |
| 2386 | type_T *type; |
| 2387 | |
| 2388 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2389 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2390 | goto theend; |
| 2391 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2392 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2393 | // 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] | 2394 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2395 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2396 | res = generate_UCALL(cctx, name, argcount); |
| 2397 | else |
| 2398 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2399 | |
| 2400 | theend: |
| 2401 | vim_free(tofree); |
| 2402 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2406 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2407 | |
| 2408 | /* |
| 2409 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2410 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2411 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2412 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2413 | * valid name. |
| 2414 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2415 | static char_u * |
| 2416 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2417 | { |
| 2418 | char_u *p; |
| 2419 | |
| 2420 | // Quick check for valid starting character. |
| 2421 | if (!eval_isnamec1(*arg)) |
| 2422 | return arg; |
| 2423 | |
| 2424 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2425 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2426 | // and can be used in slice "[n:]". |
| 2427 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2428 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2429 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2430 | break; |
| 2431 | return p; |
| 2432 | } |
| 2433 | |
| 2434 | /* |
| 2435 | * 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] | 2436 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2437 | */ |
| 2438 | char_u * |
| 2439 | to_name_const_end(char_u *arg) |
| 2440 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2441 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2442 | typval_T rettv; |
| 2443 | |
| 2444 | if (p == arg && *arg == '[') |
| 2445 | { |
| 2446 | |
| 2447 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2448 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2449 | p = arg; |
| 2450 | } |
| 2451 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2452 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2453 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2454 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2455 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2456 | p = arg; |
| 2457 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2458 | |
| 2459 | return p; |
| 2460 | } |
| 2461 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2462 | /* |
| 2463 | * parse a list: [expr, expr] |
| 2464 | * "*arg" points to the '['. |
| 2465 | */ |
| 2466 | static int |
| 2467 | compile_list(char_u **arg, cctx_T *cctx) |
| 2468 | { |
| 2469 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2470 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2471 | int count = 0; |
| 2472 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2473 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2474 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2475 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2476 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2477 | semsg(_(e_list_end), *arg); |
| 2478 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2479 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2480 | if (*p == ',') |
| 2481 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2482 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2483 | return FAIL; |
| 2484 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2485 | if (*p == ']') |
| 2486 | { |
| 2487 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2488 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2489 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2490 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2491 | break; |
| 2492 | ++count; |
| 2493 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2494 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2495 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2496 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2497 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2498 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2499 | return FAIL; |
| 2500 | } |
| 2501 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2502 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2503 | p = skipwhite(p); |
| 2504 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2505 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2506 | |
| 2507 | generate_NEWLIST(cctx, count); |
| 2508 | return OK; |
| 2509 | } |
| 2510 | |
| 2511 | /* |
| 2512 | * parse a lambda: {arg, arg -> expr} |
| 2513 | * "*arg" points to the '{'. |
| 2514 | */ |
| 2515 | static int |
| 2516 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2517 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2518 | typval_T rettv; |
| 2519 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2520 | evalarg_T evalarg; |
| 2521 | |
| 2522 | CLEAR_FIELD(evalarg); |
| 2523 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2524 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2525 | |
| 2526 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2527 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2528 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2529 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2530 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2531 | ++ufunc->uf_refcount; |
| 2532 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2533 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2534 | |
| 2535 | // The function will have one line: "return {expr}". |
| 2536 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2537 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2538 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2539 | clear_evalarg(&evalarg, NULL); |
| 2540 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2541 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2542 | { |
| 2543 | // The return type will now be known. |
| 2544 | set_function_type(ufunc); |
| 2545 | |
| 2546 | return generate_FUNCREF(cctx, ufunc); |
| 2547 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2548 | |
| 2549 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2550 | return FAIL; |
| 2551 | } |
| 2552 | |
| 2553 | /* |
| 2554 | * Compile a lamda call: expr->{lambda}(args) |
| 2555 | * "arg" points to the "{". |
| 2556 | */ |
| 2557 | static int |
| 2558 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2559 | { |
| 2560 | ufunc_T *ufunc; |
| 2561 | typval_T rettv; |
| 2562 | int argcount = 1; |
| 2563 | int ret = FAIL; |
| 2564 | |
| 2565 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2566 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2567 | return FAIL; |
| 2568 | |
| 2569 | if (**arg != '(') |
| 2570 | { |
| 2571 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2572 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2573 | else |
| 2574 | semsg(_(e_missing_paren), "lambda"); |
| 2575 | clear_tv(&rettv); |
| 2576 | return FAIL; |
| 2577 | } |
| 2578 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2579 | ufunc = rettv.vval.v_partial->pt_func; |
| 2580 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2581 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2582 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2583 | |
| 2584 | // The function will have one line: "return {expr}". |
| 2585 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2586 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2587 | |
| 2588 | // compile the arguments |
| 2589 | *arg = skipwhite(*arg + 1); |
| 2590 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2591 | // call the compiled function |
| 2592 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2593 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2594 | if (ret == FAIL) |
| 2595 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2596 | return ret; |
| 2597 | } |
| 2598 | |
| 2599 | /* |
| 2600 | * parse a dict: {'key': val} or #{key: val} |
| 2601 | * "*arg" points to the '{'. |
| 2602 | */ |
| 2603 | static int |
| 2604 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2605 | { |
| 2606 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2607 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2608 | int count = 0; |
| 2609 | dict_T *d = dict_alloc(); |
| 2610 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2611 | char_u *whitep = *arg; |
| 2612 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2613 | |
| 2614 | if (d == NULL) |
| 2615 | return FAIL; |
| 2616 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2617 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2618 | { |
| 2619 | char_u *key = NULL; |
| 2620 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2621 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2622 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2623 | *arg = NULL; |
| 2624 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | if (**arg == '}') |
| 2628 | break; |
| 2629 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2630 | if (literal) |
| 2631 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2632 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2633 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2634 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2635 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2636 | semsg(_(e_invalid_key_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2637 | return FAIL; |
| 2638 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2639 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2640 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2641 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2642 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2643 | } |
| 2644 | else |
| 2645 | { |
| 2646 | isn_T *isn; |
| 2647 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2648 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2649 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2650 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2651 | if (isn->isn_type == ISN_PUSHS) |
| 2652 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2653 | else |
| 2654 | { |
| 2655 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2656 | [stack->ga_len - 1]; |
| 2657 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2658 | return FAIL; |
| 2659 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | // Check for duplicate keys, if using string keys. |
| 2663 | if (key != NULL) |
| 2664 | { |
| 2665 | item = dict_find(d, key, -1); |
| 2666 | if (item != NULL) |
| 2667 | { |
| 2668 | semsg(_(e_duplicate_key), key); |
| 2669 | goto failret; |
| 2670 | } |
| 2671 | item = dictitem_alloc(key); |
| 2672 | if (item != NULL) |
| 2673 | { |
| 2674 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2675 | item->di_tv.v_lock = 0; |
| 2676 | if (dict_add(d, item) == FAIL) |
| 2677 | dictitem_free(item); |
| 2678 | } |
| 2679 | } |
| 2680 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2681 | if (**arg != ':') |
| 2682 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2683 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2684 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2685 | else |
| 2686 | semsg(_(e_missing_dict_colon), *arg); |
| 2687 | return FAIL; |
| 2688 | } |
| 2689 | whitep = *arg + 1; |
| 2690 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 2691 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2692 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2693 | return FAIL; |
| 2694 | } |
| 2695 | |
| 2696 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2697 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2698 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2699 | *arg = NULL; |
| 2700 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2701 | } |
| 2702 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2703 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2704 | return FAIL; |
| 2705 | ++count; |
| 2706 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2707 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2708 | *arg = skipwhite(*arg); |
| 2709 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2710 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2711 | *arg = NULL; |
| 2712 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2713 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2714 | if (**arg == '}') |
| 2715 | break; |
| 2716 | if (**arg != ',') |
| 2717 | { |
| 2718 | semsg(_(e_missing_dict_comma), *arg); |
| 2719 | goto failret; |
| 2720 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2721 | if (IS_WHITE_OR_NUL(*whitep)) |
| 2722 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2723 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2724 | return FAIL; |
| 2725 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2726 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2727 | *arg = skipwhite(*arg + 1); |
| 2728 | } |
| 2729 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2730 | *arg = *arg + 1; |
| 2731 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2732 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2733 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2734 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2735 | *arg += STRLEN(*arg); |
| 2736 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2737 | dict_unref(d); |
| 2738 | return generate_NEWDICT(cctx, count); |
| 2739 | |
| 2740 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2741 | if (*arg == NULL) |
| 2742 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2743 | dict_unref(d); |
| 2744 | return FAIL; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Compile "&option". |
| 2749 | */ |
| 2750 | static int |
| 2751 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2752 | { |
| 2753 | typval_T rettv; |
| 2754 | char_u *start = *arg; |
| 2755 | int ret; |
| 2756 | |
| 2757 | // parse the option and get the current value to get the type. |
| 2758 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2759 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2760 | if (ret == OK) |
| 2761 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2762 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2763 | char_u *name = vim_strnsave(start, *arg - start); |
| 2764 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2765 | |
| 2766 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2767 | vim_free(name); |
| 2768 | } |
| 2769 | clear_tv(&rettv); |
| 2770 | |
| 2771 | return ret; |
| 2772 | } |
| 2773 | |
| 2774 | /* |
| 2775 | * Compile "$VAR". |
| 2776 | */ |
| 2777 | static int |
| 2778 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2779 | { |
| 2780 | char_u *start = *arg; |
| 2781 | int len; |
| 2782 | int ret; |
| 2783 | char_u *name; |
| 2784 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2785 | ++*arg; |
| 2786 | len = get_env_len(arg); |
| 2787 | if (len == 0) |
| 2788 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2789 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2790 | return FAIL; |
| 2791 | } |
| 2792 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2793 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2794 | name = vim_strnsave(start, len + 1); |
| 2795 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2796 | vim_free(name); |
| 2797 | return ret; |
| 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | * Compile "@r". |
| 2802 | */ |
| 2803 | static int |
| 2804 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2805 | { |
| 2806 | int ret; |
| 2807 | |
| 2808 | ++*arg; |
| 2809 | if (**arg == NUL) |
| 2810 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2811 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2812 | return FAIL; |
| 2813 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2814 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2815 | { |
| 2816 | emsg_invreg(**arg); |
| 2817 | return FAIL; |
| 2818 | } |
| 2819 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2820 | ++*arg; |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | /* |
| 2825 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2826 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2827 | */ |
| 2828 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2829 | 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] | 2830 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2831 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2832 | |
| 2833 | // this works from end to start |
| 2834 | while (p > start) |
| 2835 | { |
| 2836 | --p; |
| 2837 | if (*p == '-' || *p == '+') |
| 2838 | { |
| 2839 | // only '-' has an effect, for '+' we only check the type |
| 2840 | #ifdef FEAT_FLOAT |
| 2841 | if (rettv->v_type == VAR_FLOAT) |
| 2842 | { |
| 2843 | if (*p == '-') |
| 2844 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2845 | } |
| 2846 | else |
| 2847 | #endif |
| 2848 | { |
| 2849 | varnumber_T val; |
| 2850 | int error = FALSE; |
| 2851 | |
| 2852 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2853 | // here |
| 2854 | if (check_not_string(rettv) == FAIL) |
| 2855 | return FAIL; |
| 2856 | val = tv_get_number_chk(rettv, &error); |
| 2857 | clear_tv(rettv); |
| 2858 | if (error) |
| 2859 | return FAIL; |
| 2860 | if (*p == '-') |
| 2861 | val = -val; |
| 2862 | rettv->v_type = VAR_NUMBER; |
| 2863 | rettv->vval.v_number = val; |
| 2864 | } |
| 2865 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2866 | else if (numeric_only) |
| 2867 | { |
| 2868 | ++p; |
| 2869 | break; |
| 2870 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2871 | else |
| 2872 | { |
| 2873 | int v = tv2bool(rettv); |
| 2874 | |
| 2875 | // '!' is permissive in the type. |
| 2876 | clear_tv(rettv); |
| 2877 | rettv->v_type = VAR_BOOL; |
| 2878 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2879 | } |
| 2880 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2881 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2882 | return OK; |
| 2883 | } |
| 2884 | |
| 2885 | /* |
| 2886 | * Recognize v: variables that are constants and set "rettv". |
| 2887 | */ |
| 2888 | static void |
| 2889 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2890 | { |
| 2891 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2892 | { |
| 2893 | rettv->v_type = VAR_BOOL; |
| 2894 | rettv->vval.v_number = VVAL_TRUE; |
| 2895 | *arg += 6; |
| 2896 | } |
| 2897 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2898 | { |
| 2899 | rettv->v_type = VAR_BOOL; |
| 2900 | rettv->vval.v_number = VVAL_FALSE; |
| 2901 | *arg += 7; |
| 2902 | } |
| 2903 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2904 | { |
| 2905 | rettv->v_type = VAR_SPECIAL; |
| 2906 | rettv->vval.v_number = VVAL_NULL; |
| 2907 | *arg += 6; |
| 2908 | } |
| 2909 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2910 | { |
| 2911 | rettv->v_type = VAR_SPECIAL; |
| 2912 | rettv->vval.v_number = VVAL_NONE; |
| 2913 | *arg += 6; |
| 2914 | } |
| 2915 | } |
| 2916 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2917 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2918 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2919 | { |
| 2920 | exptype_T type = EXPR_UNKNOWN; |
| 2921 | int i; |
| 2922 | |
| 2923 | switch (p[0]) |
| 2924 | { |
| 2925 | case '=': if (p[1] == '=') |
| 2926 | type = EXPR_EQUAL; |
| 2927 | else if (p[1] == '~') |
| 2928 | type = EXPR_MATCH; |
| 2929 | break; |
| 2930 | case '!': if (p[1] == '=') |
| 2931 | type = EXPR_NEQUAL; |
| 2932 | else if (p[1] == '~') |
| 2933 | type = EXPR_NOMATCH; |
| 2934 | break; |
| 2935 | case '>': if (p[1] != '=') |
| 2936 | { |
| 2937 | type = EXPR_GREATER; |
| 2938 | *len = 1; |
| 2939 | } |
| 2940 | else |
| 2941 | type = EXPR_GEQUAL; |
| 2942 | break; |
| 2943 | case '<': if (p[1] != '=') |
| 2944 | { |
| 2945 | type = EXPR_SMALLER; |
| 2946 | *len = 1; |
| 2947 | } |
| 2948 | else |
| 2949 | type = EXPR_SEQUAL; |
| 2950 | break; |
| 2951 | case 'i': if (p[1] == 's') |
| 2952 | { |
| 2953 | // "is" and "isnot"; but not a prefix of a name |
| 2954 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2955 | *len = 5; |
| 2956 | i = p[*len]; |
| 2957 | if (!isalnum(i) && i != '_') |
| 2958 | { |
| 2959 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2960 | *type_is = TRUE; |
| 2961 | } |
| 2962 | } |
| 2963 | break; |
| 2964 | } |
| 2965 | return type; |
| 2966 | } |
| 2967 | |
| 2968 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2969 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2970 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2971 | */ |
| 2972 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2973 | 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] | 2974 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2975 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2976 | |
| 2977 | // this works from end to start |
| 2978 | while (p > start) |
| 2979 | { |
| 2980 | --p; |
| 2981 | if (*p == '-' || *p == '+') |
| 2982 | { |
| 2983 | int negate = *p == '-'; |
| 2984 | isn_T *isn; |
| 2985 | |
| 2986 | // TODO: check type |
| 2987 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2988 | { |
| 2989 | --p; |
| 2990 | if (*p == '-') |
| 2991 | negate = !negate; |
| 2992 | } |
| 2993 | // only '-' has an effect, for '+' we only check the type |
| 2994 | if (negate) |
| 2995 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 2996 | else |
| 2997 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 2998 | if (isn == NULL) |
| 2999 | return FAIL; |
| 3000 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3001 | else if (numeric_only) |
| 3002 | { |
| 3003 | ++p; |
| 3004 | break; |
| 3005 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3006 | else |
| 3007 | { |
| 3008 | int invert = TRUE; |
| 3009 | |
| 3010 | while (p > start && p[-1] == '!') |
| 3011 | { |
| 3012 | --p; |
| 3013 | invert = !invert; |
| 3014 | } |
| 3015 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3016 | return FAIL; |
| 3017 | } |
| 3018 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3019 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3020 | return OK; |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3025 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3026 | */ |
| 3027 | static int |
| 3028 | compile_subscript( |
| 3029 | char_u **arg, |
| 3030 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3031 | char_u *start_leader, |
| 3032 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3033 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3034 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3035 | char_u *name_start = *end_leader; |
| 3036 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | for (;;) |
| 3038 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3039 | char_u *p = skipwhite(*arg); |
| 3040 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3041 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3042 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3043 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3044 | |
| 3045 | // If a following line starts with "->{" or "->X" advance to that |
| 3046 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3047 | // Also if a following line starts with ".x". |
| 3048 | if (next != NULL && |
| 3049 | ((next[0] == '-' && next[1] == '>' |
| 3050 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3051 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3052 | { |
| 3053 | next = next_line_from_context(cctx, TRUE); |
| 3054 | if (next == NULL) |
| 3055 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3056 | *arg = next; |
| 3057 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3058 | } |
| 3059 | } |
| 3060 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3061 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3062 | // not a function call. |
| 3063 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3064 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3065 | garray_T *stack = &cctx->ctx_type_stack; |
| 3066 | type_T *type; |
| 3067 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3068 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3069 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3070 | return FAIL; |
| 3071 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3072 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3073 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3074 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3075 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3076 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3077 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3078 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3079 | return FAIL; |
| 3080 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3081 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3082 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3083 | char_u *pstart = p; |
| 3084 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3085 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3086 | return FAIL; |
| 3087 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3088 | // something->method() |
| 3089 | // Apply the '!', '-' and '+' first: |
| 3090 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3091 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3092 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3093 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3094 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3095 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3096 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3097 | if (**arg == '{') |
| 3098 | { |
| 3099 | // lambda call: list->{lambda} |
| 3100 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3101 | return FAIL; |
| 3102 | } |
| 3103 | else |
| 3104 | { |
| 3105 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3106 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3107 | if (!eval_isnamec1(*p)) |
| 3108 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3109 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3110 | return FAIL; |
| 3111 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3112 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3113 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3114 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3115 | ; |
| 3116 | if (*p != '(') |
| 3117 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3118 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3119 | return FAIL; |
| 3120 | } |
| 3121 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3122 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3123 | return FAIL; |
| 3124 | } |
| 3125 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3126 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3127 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3128 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3129 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3130 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3131 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3132 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3133 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3134 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3135 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3136 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3137 | // TODO: blob index |
| 3138 | // TODO: more arguments |
| 3139 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3140 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3141 | return FAIL; |
| 3142 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3143 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3144 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3145 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3146 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3147 | if (**arg == ':') |
| 3148 | // missing first index is equal to zero |
| 3149 | generate_PUSHNR(cctx, 0); |
| 3150 | else |
| 3151 | { |
| 3152 | if (compile_expr0(arg, cctx) == FAIL) |
| 3153 | return FAIL; |
| 3154 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3155 | return FAIL; |
| 3156 | *arg = skipwhite(*arg); |
| 3157 | } |
| 3158 | if (**arg == ':') |
| 3159 | { |
| 3160 | *arg = skipwhite(*arg + 1); |
| 3161 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3162 | return FAIL; |
| 3163 | if (**arg == ']') |
| 3164 | // missing second index is equal to end of string |
| 3165 | generate_PUSHNR(cctx, -1); |
| 3166 | else |
| 3167 | { |
| 3168 | if (compile_expr0(arg, cctx) == FAIL) |
| 3169 | return FAIL; |
| 3170 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3171 | return FAIL; |
| 3172 | *arg = skipwhite(*arg); |
| 3173 | } |
| 3174 | is_slice = TRUE; |
| 3175 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3176 | |
| 3177 | if (**arg != ']') |
| 3178 | { |
| 3179 | emsg(_(e_missbrac)); |
| 3180 | return FAIL; |
| 3181 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3182 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3183 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3184 | // We can index a list and a dict. If we don't know the type |
| 3185 | // we can use the index value type. |
| 3186 | // TODO: If we don't know use an instruction to figure it out at |
| 3187 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3188 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3189 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3190 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3191 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3192 | // If the index is a string, the variable must be a Dict. |
| 3193 | if (*typep == &t_any && valtype == &t_string) |
| 3194 | vtype = VAR_DICT; |
| 3195 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3196 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3197 | if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL) |
| 3198 | return FAIL; |
| 3199 | if (is_slice) |
| 3200 | { |
| 3201 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 3202 | if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL) |
| 3203 | return FAIL; |
| 3204 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3205 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3206 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3207 | if (vtype == VAR_DICT) |
| 3208 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3209 | if (is_slice) |
| 3210 | { |
| 3211 | emsg(_(e_cannot_slice_dictionary)); |
| 3212 | return FAIL; |
| 3213 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3214 | if ((*typep)->tt_type == VAR_DICT) |
| 3215 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3216 | else |
| 3217 | { |
| 3218 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3219 | return FAIL; |
| 3220 | *typep = &t_any; |
| 3221 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3222 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3223 | return FAIL; |
| 3224 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3225 | return FAIL; |
| 3226 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3227 | else if (vtype == VAR_STRING) |
| 3228 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3229 | *typep = &t_string; |
| 3230 | if ((is_slice |
| 3231 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3232 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3233 | return FAIL; |
| 3234 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3235 | else if (vtype == VAR_BLOB) |
| 3236 | { |
| 3237 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3238 | return FAIL; |
| 3239 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3240 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3241 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3242 | if (is_slice) |
| 3243 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3244 | if (generate_instr_drop(cctx, |
| 3245 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3246 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3247 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3248 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3249 | else |
| 3250 | { |
| 3251 | if ((*typep)->tt_type == VAR_LIST) |
| 3252 | *typep = (*typep)->tt_member; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3253 | if (generate_instr_drop(cctx, |
| 3254 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3255 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3256 | return FAIL; |
| 3257 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3258 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3259 | else |
| 3260 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3261 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3262 | return FAIL; |
| 3263 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3264 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3265 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3266 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3267 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3268 | return FAIL; |
| 3269 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3270 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3271 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3272 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3273 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3274 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3275 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3276 | while (eval_isnamec(*p)) |
| 3277 | MB_PTR_ADV(p); |
| 3278 | if (p == *arg) |
| 3279 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3280 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3281 | return FAIL; |
| 3282 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3283 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3284 | return FAIL; |
| 3285 | *arg = p; |
| 3286 | } |
| 3287 | else |
| 3288 | break; |
| 3289 | } |
| 3290 | |
| 3291 | // TODO - see handle_subscript(): |
| 3292 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3293 | // Don't do this when "Func" is already a partial that was bound |
| 3294 | // explicitly (pt_auto is FALSE). |
| 3295 | |
| 3296 | return OK; |
| 3297 | } |
| 3298 | |
| 3299 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3300 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3301 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3302 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3303 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3304 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3305 | * |
| 3306 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3307 | */ |
| 3308 | |
| 3309 | /* |
| 3310 | * number number constant |
| 3311 | * 0zFFFFFFFF Blob constant |
| 3312 | * "string" string constant |
| 3313 | * 'string' literal string constant |
| 3314 | * &option-name option value |
| 3315 | * @r register contents |
| 3316 | * identifier variable value |
| 3317 | * function() function call |
| 3318 | * $VAR environment variable |
| 3319 | * (expression) nested expression |
| 3320 | * [expr, expr] List |
| 3321 | * {key: val, key: val} Dictionary |
| 3322 | * #{key: val, key: val} Dictionary with literal keys |
| 3323 | * |
| 3324 | * Also handle: |
| 3325 | * ! in front logical NOT |
| 3326 | * - in front unary minus |
| 3327 | * + in front unary plus (ignored) |
| 3328 | * trailing (arg) funcref/partial call |
| 3329 | * trailing [] subscript in String or List |
| 3330 | * trailing .name entry in Dictionary |
| 3331 | * trailing ->name() method call |
| 3332 | */ |
| 3333 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3334 | compile_expr7( |
| 3335 | char_u **arg, |
| 3336 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3337 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3338 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3339 | char_u *start_leader, *end_leader; |
| 3340 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3341 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3342 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3343 | |
| 3344 | /* |
| 3345 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3346 | */ |
| 3347 | start_leader = *arg; |
| 3348 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3349 | *arg = skipwhite(*arg + 1); |
| 3350 | end_leader = *arg; |
| 3351 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3352 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3353 | switch (**arg) |
| 3354 | { |
| 3355 | /* |
| 3356 | * Number constant. |
| 3357 | */ |
| 3358 | case '0': // also for blob starting with 0z |
| 3359 | case '1': |
| 3360 | case '2': |
| 3361 | case '3': |
| 3362 | case '4': |
| 3363 | case '5': |
| 3364 | case '6': |
| 3365 | case '7': |
| 3366 | case '8': |
| 3367 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3368 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3369 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3370 | // Apply "-" and "+" just before the number now, right to |
| 3371 | // left. Matters especially when "->" follows. Stops at |
| 3372 | // '!'. |
| 3373 | if (apply_leader(rettv, TRUE, |
| 3374 | start_leader, &end_leader) == FAIL) |
| 3375 | { |
| 3376 | clear_tv(rettv); |
| 3377 | return FAIL; |
| 3378 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3379 | break; |
| 3380 | |
| 3381 | /* |
| 3382 | * String constant: "string". |
| 3383 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3384 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3385 | return FAIL; |
| 3386 | break; |
| 3387 | |
| 3388 | /* |
| 3389 | * Literal string constant: 'str''ing'. |
| 3390 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3391 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3392 | return FAIL; |
| 3393 | break; |
| 3394 | |
| 3395 | /* |
| 3396 | * Constant Vim variable. |
| 3397 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3398 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3399 | ret = NOTDONE; |
| 3400 | break; |
| 3401 | |
| 3402 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3403 | * "true" constant |
| 3404 | */ |
| 3405 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3406 | && !eval_isnamec((*arg)[4])) |
| 3407 | { |
| 3408 | *arg += 4; |
| 3409 | rettv->v_type = VAR_BOOL; |
| 3410 | rettv->vval.v_number = VVAL_TRUE; |
| 3411 | } |
| 3412 | else |
| 3413 | ret = NOTDONE; |
| 3414 | break; |
| 3415 | |
| 3416 | /* |
| 3417 | * "false" constant |
| 3418 | */ |
| 3419 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3420 | && !eval_isnamec((*arg)[5])) |
| 3421 | { |
| 3422 | *arg += 5; |
| 3423 | rettv->v_type = VAR_BOOL; |
| 3424 | rettv->vval.v_number = VVAL_FALSE; |
| 3425 | } |
| 3426 | else |
| 3427 | ret = NOTDONE; |
| 3428 | break; |
| 3429 | |
| 3430 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3431 | * List: [expr, expr] |
| 3432 | */ |
| 3433 | case '[': ret = compile_list(arg, cctx); |
| 3434 | break; |
| 3435 | |
| 3436 | /* |
| 3437 | * Dictionary: #{key: val, key: val} |
| 3438 | */ |
| 3439 | case '#': if ((*arg)[1] == '{') |
| 3440 | { |
| 3441 | ++*arg; |
| 3442 | ret = compile_dict(arg, cctx, TRUE); |
| 3443 | } |
| 3444 | else |
| 3445 | ret = NOTDONE; |
| 3446 | break; |
| 3447 | |
| 3448 | /* |
| 3449 | * Lambda: {arg, arg -> expr} |
| 3450 | * Dictionary: {'key': val, 'key': val} |
| 3451 | */ |
| 3452 | case '{': { |
| 3453 | char_u *start = skipwhite(*arg + 1); |
| 3454 | |
| 3455 | // Find out what comes after the arguments. |
| 3456 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3457 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3458 | if (ret != FAIL && *start == '>') |
| 3459 | ret = compile_lambda(arg, cctx); |
| 3460 | else |
| 3461 | ret = compile_dict(arg, cctx, FALSE); |
| 3462 | } |
| 3463 | break; |
| 3464 | |
| 3465 | /* |
| 3466 | * Option value: &name |
| 3467 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3468 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3469 | return FAIL; |
| 3470 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3471 | break; |
| 3472 | |
| 3473 | /* |
| 3474 | * Environment variable: $VAR. |
| 3475 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3476 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3477 | return FAIL; |
| 3478 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3479 | break; |
| 3480 | |
| 3481 | /* |
| 3482 | * Register contents: @r. |
| 3483 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3484 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3485 | return FAIL; |
| 3486 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3487 | break; |
| 3488 | /* |
| 3489 | * nested expression: (expression). |
| 3490 | */ |
| 3491 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3492 | |
| 3493 | // recursive! |
| 3494 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3495 | { |
| 3496 | ret = compile_expr1(arg, cctx, ppconst); |
| 3497 | } |
| 3498 | else |
| 3499 | { |
| 3500 | // Not enough space in ppconst, flush constants. |
| 3501 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3502 | return FAIL; |
| 3503 | ret = compile_expr0(arg, cctx); |
| 3504 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3505 | *arg = skipwhite(*arg); |
| 3506 | if (**arg == ')') |
| 3507 | ++*arg; |
| 3508 | else if (ret == OK) |
| 3509 | { |
| 3510 | emsg(_(e_missing_close)); |
| 3511 | ret = FAIL; |
| 3512 | } |
| 3513 | break; |
| 3514 | |
| 3515 | default: ret = NOTDONE; |
| 3516 | break; |
| 3517 | } |
| 3518 | if (ret == FAIL) |
| 3519 | return FAIL; |
| 3520 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3521 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3522 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3523 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3524 | clear_tv(rettv); |
| 3525 | else |
| 3526 | // A constant expression can possibly be handled compile time, |
| 3527 | // return the value instead of generating code. |
| 3528 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3529 | } |
| 3530 | else if (ret == NOTDONE) |
| 3531 | { |
| 3532 | char_u *p; |
| 3533 | int r; |
| 3534 | |
| 3535 | if (!eval_isnamec1(**arg)) |
| 3536 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3537 | semsg(_(e_name_expected), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3538 | return FAIL; |
| 3539 | } |
| 3540 | |
| 3541 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3542 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3543 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3544 | { |
| 3545 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3546 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3547 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3548 | { |
| 3549 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3550 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3551 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3552 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3553 | if (r == FAIL) |
| 3554 | return FAIL; |
| 3555 | } |
| 3556 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3557 | // Handle following "[]", ".member", etc. |
| 3558 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3559 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3560 | ppconst) == FAIL) |
| 3561 | return FAIL; |
| 3562 | if (ppconst->pp_used > 0) |
| 3563 | { |
| 3564 | // apply the '!', '-' and '+' before the constant |
| 3565 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3566 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3567 | return FAIL; |
| 3568 | return OK; |
| 3569 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3570 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3571 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3572 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3576 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3577 | */ |
| 3578 | void |
| 3579 | error_white_both(char_u *op, int len) |
| 3580 | { |
| 3581 | char_u buf[10]; |
| 3582 | |
| 3583 | vim_strncpy(buf, op, len); |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3584 | semsg(_(e_white_space_required_before_and_after_str), buf); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3588 | * <type>expr7: runtime type check / conversion |
| 3589 | */ |
| 3590 | static int |
| 3591 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3592 | { |
| 3593 | type_T *want_type = NULL; |
| 3594 | |
| 3595 | // Recognize <type> |
| 3596 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3597 | { |
| 3598 | int called_emsg_before = called_emsg; |
| 3599 | |
| 3600 | ++*arg; |
| 3601 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3602 | if (called_emsg != called_emsg_before) |
| 3603 | return FAIL; |
| 3604 | |
| 3605 | if (**arg != '>') |
| 3606 | { |
| 3607 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3608 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3609 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3610 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3611 | return FAIL; |
| 3612 | } |
| 3613 | ++*arg; |
| 3614 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3615 | return FAIL; |
| 3616 | } |
| 3617 | |
| 3618 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3619 | return FAIL; |
| 3620 | |
| 3621 | if (want_type != NULL) |
| 3622 | { |
| 3623 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3624 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3625 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3626 | generate_ppconst(cctx, ppconst); |
| 3627 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 3628 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3629 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3630 | if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL) |
| 3631 | return FAIL; |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | return OK; |
| 3636 | } |
| 3637 | |
| 3638 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3639 | * * number multiplication |
| 3640 | * / number division |
| 3641 | * % number modulo |
| 3642 | */ |
| 3643 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3644 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3645 | { |
| 3646 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3647 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3648 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3649 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3650 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3651 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3652 | return FAIL; |
| 3653 | |
| 3654 | /* |
| 3655 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3656 | */ |
| 3657 | for (;;) |
| 3658 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3659 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3660 | if (*op != '*' && *op != '/' && *op != '%') |
| 3661 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3662 | if (next != NULL) |
| 3663 | { |
| 3664 | *arg = next_line_from_context(cctx, TRUE); |
| 3665 | op = skipwhite(*arg); |
| 3666 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3667 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3668 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3669 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3670 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3671 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3672 | } |
| 3673 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3674 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3675 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3676 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3677 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3678 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3679 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3680 | |
| 3681 | if (ppconst->pp_used == ppconst_used + 2 |
| 3682 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3683 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3684 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3685 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3686 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3687 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3688 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3689 | // both are numbers: compute the result |
| 3690 | switch (*op) |
| 3691 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3692 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3693 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3694 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3695 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3696 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3697 | break; |
| 3698 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3699 | tv1->vval.v_number = res; |
| 3700 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3701 | } |
| 3702 | else |
| 3703 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3704 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3705 | generate_two_op(cctx, op); |
| 3706 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | return OK; |
| 3710 | } |
| 3711 | |
| 3712 | /* |
| 3713 | * + number addition |
| 3714 | * - number subtraction |
| 3715 | * .. string concatenation |
| 3716 | */ |
| 3717 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3718 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3719 | { |
| 3720 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3721 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3722 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3723 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3724 | |
| 3725 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3726 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3727 | return FAIL; |
| 3728 | |
| 3729 | /* |
| 3730 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3731 | */ |
| 3732 | for (;;) |
| 3733 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3734 | op = may_peek_next_line(cctx, *arg, &next); |
| 3735 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3736 | break; |
| 3737 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3738 | if (next != NULL) |
| 3739 | { |
| 3740 | *arg = next_line_from_context(cctx, TRUE); |
| 3741 | op = skipwhite(*arg); |
| 3742 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3743 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3744 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3745 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3746 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3747 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3751 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3752 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3753 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3754 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3755 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3756 | return FAIL; |
| 3757 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3758 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3759 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3760 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3761 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3762 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3763 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3764 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3765 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3766 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3767 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3768 | // concat/subtract/add constant numbers |
| 3769 | if (*op == '+') |
| 3770 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3771 | else if (*op == '-') |
| 3772 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3773 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3774 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3775 | // concatenate constant strings |
| 3776 | char_u *s1 = tv1->vval.v_string; |
| 3777 | char_u *s2 = tv2->vval.v_string; |
| 3778 | size_t len1 = STRLEN(s1); |
| 3779 | |
| 3780 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3781 | if (tv1->vval.v_string == NULL) |
| 3782 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3783 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3784 | return FAIL; |
| 3785 | } |
| 3786 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3787 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3788 | vim_free(s1); |
| 3789 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3790 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3791 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | } |
| 3793 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3794 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3795 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3796 | if (*op == '.') |
| 3797 | { |
| 3798 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3799 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3800 | return FAIL; |
| 3801 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3802 | } |
| 3803 | else |
| 3804 | generate_two_op(cctx, op); |
| 3805 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | return OK; |
| 3809 | } |
| 3810 | |
| 3811 | /* |
| 3812 | * expr5a == expr5b |
| 3813 | * expr5a =~ expr5b |
| 3814 | * expr5a != expr5b |
| 3815 | * expr5a !~ expr5b |
| 3816 | * expr5a > expr5b |
| 3817 | * expr5a >= expr5b |
| 3818 | * expr5a < expr5b |
| 3819 | * expr5a <= expr5b |
| 3820 | * expr5a is expr5b |
| 3821 | * expr5a isnot expr5b |
| 3822 | * |
| 3823 | * Produces instructions: |
| 3824 | * EVAL expr5a Push result of "expr5a" |
| 3825 | * EVAL expr5b Push result of "expr5b" |
| 3826 | * COMPARE one of the compare instructions |
| 3827 | */ |
| 3828 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3829 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3830 | { |
| 3831 | exptype_T type = EXPR_UNKNOWN; |
| 3832 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3833 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3834 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3835 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3836 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3837 | |
| 3838 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3839 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3840 | return FAIL; |
| 3841 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3842 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3843 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3844 | |
| 3845 | /* |
| 3846 | * If there is a comparative operator, use it. |
| 3847 | */ |
| 3848 | if (type != EXPR_UNKNOWN) |
| 3849 | { |
| 3850 | int ic = FALSE; // Default: do not ignore case |
| 3851 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3852 | if (next != NULL) |
| 3853 | { |
| 3854 | *arg = next_line_from_context(cctx, TRUE); |
| 3855 | p = skipwhite(*arg); |
| 3856 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3857 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3858 | { |
| 3859 | semsg(_(e_invexpr2), *arg); |
| 3860 | return FAIL; |
| 3861 | } |
| 3862 | // extra question mark appended: ignore case |
| 3863 | if (p[len] == '?') |
| 3864 | { |
| 3865 | ic = TRUE; |
| 3866 | ++len; |
| 3867 | } |
| 3868 | // extra '#' appended: match case (ignored) |
| 3869 | else if (p[len] == '#') |
| 3870 | ++len; |
| 3871 | // nothing appended: match case |
| 3872 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3873 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3874 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3875 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3876 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | // get the second variable |
| 3880 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3881 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3882 | return FAIL; |
| 3883 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3884 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3885 | return FAIL; |
| 3886 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3887 | if (ppconst->pp_used == ppconst_used + 2) |
| 3888 | { |
| 3889 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3890 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3891 | int ret; |
| 3892 | |
| 3893 | // Both sides are a constant, compute the result now. |
| 3894 | // First check for a valid combination of types, this is more |
| 3895 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3896 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3897 | ret = FAIL; |
| 3898 | else |
| 3899 | { |
| 3900 | ret = typval_compare(tv1, tv2, type, ic); |
| 3901 | tv1->v_type = VAR_BOOL; |
| 3902 | tv1->vval.v_number = tv1->vval.v_number |
| 3903 | ? VVAL_TRUE : VVAL_FALSE; |
| 3904 | clear_tv(tv2); |
| 3905 | --ppconst->pp_used; |
| 3906 | } |
| 3907 | return ret; |
| 3908 | } |
| 3909 | |
| 3910 | generate_ppconst(cctx, ppconst); |
| 3911 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | return OK; |
| 3915 | } |
| 3916 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3917 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3918 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3919 | /* |
| 3920 | * Compile || or &&. |
| 3921 | */ |
| 3922 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3923 | compile_and_or( |
| 3924 | char_u **arg, |
| 3925 | cctx_T *cctx, |
| 3926 | char *op, |
| 3927 | ppconst_T *ppconst, |
| 3928 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3929 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3930 | char_u *next; |
| 3931 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3932 | int opchar = *op; |
| 3933 | |
| 3934 | if (p[0] == opchar && p[1] == opchar) |
| 3935 | { |
| 3936 | garray_T *instr = &cctx->ctx_instr; |
| 3937 | garray_T end_ga; |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 3938 | garray_T *stack = &cctx->ctx_type_stack; |
| 3939 | type_T **typep; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3940 | |
| 3941 | /* |
| 3942 | * Repeat until there is no following "||" or "&&" |
| 3943 | */ |
| 3944 | ga_init2(&end_ga, sizeof(int), 10); |
| 3945 | while (p[0] == opchar && p[1] == opchar) |
| 3946 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3947 | if (next != NULL) |
| 3948 | { |
| 3949 | *arg = next_line_from_context(cctx, TRUE); |
| 3950 | p = skipwhite(*arg); |
| 3951 | } |
| 3952 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3953 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3954 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3955 | semsg(_(e_white_space_required_before_and_after_str), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3956 | return FAIL; |
| 3957 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3958 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3959 | // TODO: use ppconst if the value is a constant |
| 3960 | generate_ppconst(cctx, ppconst); |
| 3961 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3962 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3963 | { |
| 3964 | ga_clear(&end_ga); |
| 3965 | return FAIL; |
| 3966 | } |
| 3967 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3968 | ++end_ga.ga_len; |
| 3969 | generate_JUMP(cctx, opchar == '|' |
| 3970 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3971 | |
| 3972 | // eval the next expression |
| 3973 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3974 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3975 | return FAIL; |
| 3976 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3977 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3978 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3979 | { |
| 3980 | ga_clear(&end_ga); |
| 3981 | return FAIL; |
| 3982 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3983 | |
| 3984 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3985 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3986 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3987 | |
| 3988 | // Fill in the end label in all jumps. |
| 3989 | while (end_ga.ga_len > 0) |
| 3990 | { |
| 3991 | isn_T *isn; |
| 3992 | |
| 3993 | --end_ga.ga_len; |
| 3994 | isn = ((isn_T *)instr->ga_data) |
| 3995 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3996 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3997 | } |
| 3998 | ga_clear(&end_ga); |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 3999 | |
| 4000 | // The resulting type can be used as a bool. |
| 4001 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 4002 | if (*typep != &t_bool) |
| 4003 | { |
| 4004 | type_T *type = alloc_type(cctx->ctx_type_list); |
| 4005 | |
| 4006 | if (type != NULL) |
| 4007 | { |
| 4008 | *type = **typep; |
| 4009 | type->tt_flags |= TTFLAG_BOOL_OK; |
| 4010 | *typep = type; |
| 4011 | } |
| 4012 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | return OK; |
| 4016 | } |
| 4017 | |
| 4018 | /* |
| 4019 | * expr4a && expr4a && expr4a logical AND |
| 4020 | * |
| 4021 | * Produces instructions: |
| 4022 | * EVAL expr4a Push result of "expr4a" |
| 4023 | * JUMP_AND_KEEP_IF_FALSE end |
| 4024 | * EVAL expr4b Push result of "expr4b" |
| 4025 | * JUMP_AND_KEEP_IF_FALSE end |
| 4026 | * EVAL expr4c Push result of "expr4c" |
| 4027 | * end: |
| 4028 | */ |
| 4029 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4030 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4031 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4032 | int ppconst_used = ppconst->pp_used; |
| 4033 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4034 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4035 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4036 | return FAIL; |
| 4037 | |
| 4038 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4039 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | /* |
| 4043 | * expr3a || expr3b || expr3c logical OR |
| 4044 | * |
| 4045 | * Produces instructions: |
| 4046 | * EVAL expr3a Push result of "expr3a" |
| 4047 | * JUMP_AND_KEEP_IF_TRUE end |
| 4048 | * EVAL expr3b Push result of "expr3b" |
| 4049 | * JUMP_AND_KEEP_IF_TRUE end |
| 4050 | * EVAL expr3c Push result of "expr3c" |
| 4051 | * end: |
| 4052 | */ |
| 4053 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4054 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4055 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4056 | int ppconst_used = ppconst->pp_used; |
| 4057 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4058 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4059 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4060 | return FAIL; |
| 4061 | |
| 4062 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4063 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | /* |
| 4067 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4068 | * |
| 4069 | * Produces instructions: |
| 4070 | * EVAL expr2 Push result of "expr" |
| 4071 | * JUMP_IF_FALSE alt jump if false |
| 4072 | * EVAL expr1a |
| 4073 | * JUMP_ALWAYS end |
| 4074 | * alt: EVAL expr1b |
| 4075 | * end: |
| 4076 | */ |
| 4077 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4078 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4079 | { |
| 4080 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4081 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4082 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4083 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4084 | // Ignore all kinds of errors when not producing code. |
| 4085 | if (cctx->ctx_skip == SKIP_YES) |
| 4086 | { |
| 4087 | skip_expr(arg); |
| 4088 | return OK; |
| 4089 | } |
| 4090 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4091 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4092 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4093 | return FAIL; |
| 4094 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4095 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | if (*p == '?') |
| 4097 | { |
| 4098 | garray_T *instr = &cctx->ctx_instr; |
| 4099 | garray_T *stack = &cctx->ctx_type_stack; |
| 4100 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4101 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4102 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4103 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4104 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4105 | int has_const_expr = FALSE; |
| 4106 | int const_value = FALSE; |
| 4107 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4108 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4109 | if (next != NULL) |
| 4110 | { |
| 4111 | *arg = next_line_from_context(cctx, TRUE); |
| 4112 | p = skipwhite(*arg); |
| 4113 | } |
| 4114 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4115 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4116 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4117 | semsg(_(e_white_space_required_before_and_after_str), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4118 | return FAIL; |
| 4119 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4120 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4121 | if (ppconst->pp_used == ppconst_used + 1) |
| 4122 | { |
| 4123 | // the condition is a constant, we know whether the ? or the : |
| 4124 | // expression is to be evaluated. |
| 4125 | has_const_expr = TRUE; |
| 4126 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4127 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4128 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4129 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4130 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4131 | } |
| 4132 | else |
| 4133 | { |
| 4134 | generate_ppconst(cctx, ppconst); |
| 4135 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4136 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4137 | |
| 4138 | // evaluate the second expression; any type is accepted |
| 4139 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4140 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4141 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4142 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4143 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4144 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4145 | if (!has_const_expr) |
| 4146 | { |
| 4147 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4148 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4149 | // remember the type and drop it |
| 4150 | --stack->ga_len; |
| 4151 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4152 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4153 | end_idx = instr->ga_len; |
| 4154 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4155 | |
| 4156 | // jump here from JUMP_IF_FALSE |
| 4157 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4158 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4159 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4160 | |
| 4161 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4162 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4163 | if (*p != ':') |
| 4164 | { |
| 4165 | emsg(_(e_missing_colon)); |
| 4166 | return FAIL; |
| 4167 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4168 | if (next != NULL) |
| 4169 | { |
| 4170 | *arg = next_line_from_context(cctx, TRUE); |
| 4171 | p = skipwhite(*arg); |
| 4172 | } |
| 4173 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4174 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4175 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4176 | semsg(_(e_white_space_required_before_and_after_str), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4177 | return FAIL; |
| 4178 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | |
| 4180 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4181 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4182 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4183 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4184 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4185 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4186 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4187 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4188 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4189 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4190 | if (!has_const_expr) |
| 4191 | { |
| 4192 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4193 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4194 | // If the types differ, the result has a more generic type. |
| 4195 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4196 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4197 | |
| 4198 | // jump here from JUMP_ALWAYS |
| 4199 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4200 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4201 | } |
| 4202 | |
| 4203 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4204 | } |
| 4205 | return OK; |
| 4206 | } |
| 4207 | |
| 4208 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4209 | * Toplevel expression. |
| 4210 | */ |
| 4211 | static int |
| 4212 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4213 | { |
| 4214 | ppconst_T ppconst; |
| 4215 | |
| 4216 | CLEAR_FIELD(ppconst); |
| 4217 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4218 | { |
| 4219 | clear_ppconst(&ppconst); |
| 4220 | return FAIL; |
| 4221 | } |
| 4222 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4223 | return FAIL; |
| 4224 | return OK; |
| 4225 | } |
| 4226 | |
| 4227 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4228 | * compile "return [expr]" |
| 4229 | */ |
| 4230 | static char_u * |
| 4231 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4232 | { |
| 4233 | char_u *p = arg; |
| 4234 | garray_T *stack = &cctx->ctx_type_stack; |
| 4235 | type_T *stack_type; |
| 4236 | |
| 4237 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4238 | { |
| 4239 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4240 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4241 | return NULL; |
| 4242 | |
| 4243 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4244 | if (set_return_type) |
| 4245 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4246 | else |
| 4247 | { |
| 4248 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4249 | && stack_type->tt_type != VAR_VOID |
| 4250 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4251 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4252 | emsg(_(e_returning_value_in_function_without_return_type)); |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4253 | return NULL; |
| 4254 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4255 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4256 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4257 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4258 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4259 | } |
| 4260 | else |
| 4261 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4262 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4263 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4264 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4265 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4266 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4267 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4268 | return NULL; |
| 4269 | } |
| 4270 | |
| 4271 | // No argument, return zero. |
| 4272 | generate_PUSHNR(cctx, 0); |
| 4273 | } |
| 4274 | |
| 4275 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4276 | return NULL; |
| 4277 | |
| 4278 | // "return val | endif" is possible |
| 4279 | return skipwhite(p); |
| 4280 | } |
| 4281 | |
| 4282 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4283 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4284 | * Return a pointer to the line in allocated memory. |
| 4285 | * Return NULL for end-of-file or some error. |
| 4286 | */ |
| 4287 | static char_u * |
| 4288 | exarg_getline( |
| 4289 | int c UNUSED, |
| 4290 | void *cookie, |
| 4291 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4292 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4293 | { |
| 4294 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4295 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4296 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4297 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4298 | { |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4299 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4300 | return NULL; |
| 4301 | ++cctx->ctx_lnum; |
| 4302 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4303 | // Comment lines result in NULL pointers, skip them. |
| 4304 | if (p != NULL) |
| 4305 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4306 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | /* |
| 4310 | * Compile a nested :def command. |
| 4311 | */ |
| 4312 | static char_u * |
| 4313 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4314 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4315 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4316 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4317 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4318 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4319 | lvar_T *lvar; |
| 4320 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4321 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4322 | |
Bram Moolenaar | 8b848ca | 2020-09-10 22:28:01 +0200 | [diff] [blame] | 4323 | if (*name_start == '!') |
| 4324 | { |
| 4325 | emsg(_(e_cannot_use_bang_with_nested_def)); |
| 4326 | return NULL; |
| 4327 | } |
| 4328 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4329 | // Only g:Func() can use a namespace. |
| 4330 | if (name_start[1] == ':' && !is_global) |
| 4331 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4332 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4333 | return NULL; |
| 4334 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4335 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4336 | return NULL; |
| 4337 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4338 | eap->arg = name_end; |
| 4339 | eap->getline = exarg_getline; |
| 4340 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4341 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4342 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4343 | lambda_name = get_lambda_name(); |
| 4344 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4345 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4346 | if (ufunc == NULL) |
Bram Moolenaar | 7a3330f | 2020-08-27 23:57:57 +0200 | [diff] [blame] | 4347 | return eap->skip ? (char_u *)"" : NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4348 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4349 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4350 | return NULL; |
| 4351 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4352 | if (is_global) |
| 4353 | { |
| 4354 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4355 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4356 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4357 | if (func_name == NULL) |
| 4358 | r = FAIL; |
| 4359 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4360 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4361 | } |
| 4362 | else |
| 4363 | { |
| 4364 | // Define a local variable for the function reference. |
| 4365 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4366 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4367 | if (lvar == NULL) |
| 4368 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4369 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4370 | return NULL; |
| 4371 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4372 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4373 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4374 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4375 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4379 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4380 | */ |
| 4381 | int |
| 4382 | assignment_len(char_u *p, int *heredoc) |
| 4383 | { |
| 4384 | if (*p == '=') |
| 4385 | { |
| 4386 | if (p[1] == '<' && p[2] == '<') |
| 4387 | { |
| 4388 | *heredoc = TRUE; |
| 4389 | return 3; |
| 4390 | } |
| 4391 | return 1; |
| 4392 | } |
| 4393 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4394 | return 2; |
| 4395 | if (STRNCMP(p, "..=", 3) == 0) |
| 4396 | return 3; |
| 4397 | return 0; |
| 4398 | } |
| 4399 | |
| 4400 | // words that cannot be used as a variable |
| 4401 | static char *reserved[] = { |
| 4402 | "true", |
| 4403 | "false", |
| 4404 | NULL |
| 4405 | }; |
| 4406 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4407 | typedef enum { |
| 4408 | dest_local, |
| 4409 | dest_option, |
| 4410 | dest_env, |
| 4411 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4412 | dest_buffer, |
| 4413 | dest_window, |
| 4414 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4415 | dest_vimvar, |
| 4416 | dest_script, |
| 4417 | dest_reg, |
| 4418 | } assign_dest_T; |
| 4419 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4420 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4421 | * Generate the load instruction for "name". |
| 4422 | */ |
| 4423 | static void |
| 4424 | generate_loadvar( |
| 4425 | cctx_T *cctx, |
| 4426 | assign_dest_T dest, |
| 4427 | char_u *name, |
| 4428 | lvar_T *lvar, |
| 4429 | type_T *type) |
| 4430 | { |
| 4431 | switch (dest) |
| 4432 | { |
| 4433 | case dest_option: |
| 4434 | // TODO: check the option exists |
| 4435 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4436 | break; |
| 4437 | case dest_global: |
| 4438 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4439 | break; |
| 4440 | case dest_buffer: |
| 4441 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4442 | break; |
| 4443 | case dest_window: |
| 4444 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4445 | break; |
| 4446 | case dest_tab: |
| 4447 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4448 | break; |
| 4449 | case dest_script: |
| 4450 | compile_load_scriptvar(cctx, |
| 4451 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4452 | break; |
| 4453 | case dest_env: |
| 4454 | // Include $ in the name here |
| 4455 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4456 | break; |
| 4457 | case dest_reg: |
| 4458 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4459 | break; |
| 4460 | case dest_vimvar: |
| 4461 | generate_LOADV(cctx, name + 2, TRUE); |
| 4462 | break; |
| 4463 | case dest_local: |
| 4464 | if (lvar->lv_from_outer) |
| 4465 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4466 | NULL, type); |
| 4467 | else |
| 4468 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4469 | break; |
| 4470 | } |
| 4471 | } |
| 4472 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4473 | void |
| 4474 | vim9_declare_error(char_u *name) |
| 4475 | { |
| 4476 | char *scope = ""; |
| 4477 | |
| 4478 | switch (*name) |
| 4479 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4480 | case 'g': scope = _("global"); break; |
| 4481 | case 'b': scope = _("buffer"); break; |
| 4482 | case 'w': scope = _("window"); break; |
| 4483 | case 't': scope = _("tab"); break; |
| 4484 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4485 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4486 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4487 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4488 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4489 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4490 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4491 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4492 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4493 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4494 | } |
| 4495 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4496 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4497 | * Compile declaration and assignment: |
| 4498 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4499 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4500 | * Return NULL for an error. |
| 4501 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4502 | */ |
| 4503 | static char_u * |
| 4504 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4505 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4506 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4507 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4508 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4509 | char_u *ret = NULL; |
| 4510 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4511 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4512 | int scriptvar_sid = 0; |
| 4513 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4514 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4515 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4516 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4517 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4518 | int oplen = 0; |
| 4519 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4520 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4521 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4522 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4523 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4524 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4525 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4526 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4527 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4528 | if (p == NULL) |
| 4529 | return *arg == '[' ? arg : NULL; |
| 4530 | |
| 4531 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4532 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4533 | // TODO: should we allow this, and figure out type inference from list |
| 4534 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4535 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4536 | return NULL; |
| 4537 | } |
| 4538 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4539 | sp = p; |
| 4540 | p = skipwhite(p); |
| 4541 | op = p; |
| 4542 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4543 | |
| 4544 | if (var_count > 0 && oplen == 0) |
| 4545 | // can be something like "[1, 2]->func()" |
| 4546 | return arg; |
| 4547 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4548 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4549 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4550 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4551 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4552 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4553 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4554 | if (heredoc) |
| 4555 | { |
| 4556 | list_T *l; |
| 4557 | listitem_T *li; |
| 4558 | |
| 4559 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4560 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4561 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4562 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4563 | |
| 4564 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4565 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4566 | { |
| 4567 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4568 | li->li_tv.vval.v_string = NULL; |
| 4569 | } |
| 4570 | generate_NEWLIST(cctx, l->lv_len); |
| 4571 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4572 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4573 | list_free(l); |
| 4574 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4575 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4576 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4577 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4578 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4579 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4580 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4581 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4582 | p = skipwhite(op + oplen); |
| 4583 | if (compile_expr0(&p, cctx) == FAIL) |
| 4584 | return NULL; |
| 4585 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4586 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4587 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4588 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4589 | type_T *stacktype; |
| 4590 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4591 | stacktype = stack->ga_len == 0 ? &t_void |
| 4592 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4593 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4594 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4595 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4596 | goto theend; |
| 4597 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4598 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4599 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4600 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4601 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4602 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | /* |
| 4607 | * Loop over variables in "[var, var] = expr". |
| 4608 | * For "var = expr" and "let var: type" this is done only once. |
| 4609 | */ |
| 4610 | if (var_count > 0) |
| 4611 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4612 | else |
| 4613 | var_start = arg; |
| 4614 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4615 | { |
| 4616 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4617 | size_t varlen; |
| 4618 | int new_local = FALSE; |
| 4619 | int opt_type; |
| 4620 | int opt_flags = 0; |
| 4621 | assign_dest_T dest = dest_local; |
| 4622 | int vimvaridx = -1; |
| 4623 | lvar_T *lvar = NULL; |
| 4624 | lvar_T arg_lvar; |
| 4625 | int has_type = FALSE; |
| 4626 | int has_index = FALSE; |
| 4627 | int instr_count = -1; |
| 4628 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4629 | if (*var_start == '@') |
| 4630 | p = var_start + 2; |
| 4631 | else |
| 4632 | { |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4633 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 4634 | p = skip_option_env_lead(var_start); |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4635 | p = to_name_end(p, TRUE); |
| 4636 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4637 | |
| 4638 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4639 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4640 | --var_end; |
| 4641 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4642 | --p; |
| 4643 | |
| 4644 | varlen = p - var_start; |
| 4645 | vim_free(name); |
| 4646 | name = vim_strnsave(var_start, varlen); |
| 4647 | if (name == NULL) |
| 4648 | return NULL; |
| 4649 | if (!heredoc) |
| 4650 | type = &t_any; |
| 4651 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4652 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4653 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4654 | int declare_error = FALSE; |
| 4655 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4656 | if (*var_start == '&') |
| 4657 | { |
| 4658 | int cc; |
| 4659 | long numval; |
| 4660 | |
| 4661 | dest = dest_option; |
| 4662 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4663 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4664 | emsg(_(e_const_option)); |
| 4665 | goto theend; |
| 4666 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4667 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4668 | p = var_start; |
| 4669 | p = find_option_end(&p, &opt_flags); |
| 4670 | if (p == NULL) |
| 4671 | { |
| 4672 | // cannot happen? |
| 4673 | emsg(_(e_letunexp)); |
| 4674 | goto theend; |
| 4675 | } |
| 4676 | cc = *p; |
| 4677 | *p = NUL; |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4678 | opt_type = get_option_value(skip_option_env_lead(var_start), |
| 4679 | &numval, NULL, opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4680 | *p = cc; |
| 4681 | if (opt_type == -3) |
| 4682 | { |
| 4683 | semsg(_(e_unknown_option), var_start); |
| 4684 | goto theend; |
| 4685 | } |
| 4686 | if (opt_type == -2 || opt_type == 0) |
| 4687 | type = &t_string; |
| 4688 | else |
| 4689 | type = &t_number; // both number and boolean option |
| 4690 | } |
| 4691 | else if (*var_start == '$') |
| 4692 | { |
| 4693 | dest = dest_env; |
| 4694 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4695 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4696 | } |
| 4697 | else if (*var_start == '@') |
| 4698 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4699 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4700 | { |
| 4701 | emsg_invreg(var_start[1]); |
| 4702 | goto theend; |
| 4703 | } |
| 4704 | dest = dest_reg; |
| 4705 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4706 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4707 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4708 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4709 | { |
| 4710 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4711 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4712 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4713 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4714 | { |
| 4715 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4716 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4717 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4718 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4719 | { |
| 4720 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4721 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4722 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4723 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4724 | { |
| 4725 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4726 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4727 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4728 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4729 | { |
| 4730 | typval_T *vtv; |
| 4731 | int di_flags; |
| 4732 | |
| 4733 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4734 | if (vimvaridx < 0) |
| 4735 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4736 | semsg(_(e_variable_not_found_str), var_start); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4737 | goto theend; |
| 4738 | } |
| 4739 | // We use the current value of "sandbox" here, is that OK? |
| 4740 | if (var_check_ro(di_flags, name, FALSE)) |
| 4741 | goto theend; |
| 4742 | dest = dest_vimvar; |
| 4743 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4744 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4745 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4746 | } |
| 4747 | else |
| 4748 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4749 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4750 | |
| 4751 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4752 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4753 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4754 | semsg(_(e_cannot_use_reserved_name), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4755 | goto theend; |
| 4756 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4757 | |
| 4758 | lvar = lookup_local(var_start, varlen, cctx); |
| 4759 | if (lvar == NULL) |
| 4760 | { |
| 4761 | CLEAR_FIELD(arg_lvar); |
| 4762 | if (lookup_arg(var_start, varlen, |
| 4763 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4764 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4765 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4766 | if (is_decl) |
| 4767 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4768 | semsg(_(e_str_is_used_as_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4769 | goto theend; |
| 4770 | } |
| 4771 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4772 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4773 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4774 | if (lvar != NULL) |
| 4775 | { |
| 4776 | if (is_decl) |
| 4777 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4778 | semsg(_(e_variable_already_declared), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4779 | goto theend; |
| 4780 | } |
| 4781 | else if (lvar->lv_const) |
| 4782 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4783 | semsg(_(e_cannot_assign_to_constant), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4784 | goto theend; |
| 4785 | } |
| 4786 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4787 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4788 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4789 | int script_namespace = varlen > 1 |
| 4790 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4791 | int script_var = (script_namespace |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 4792 | ? lookup_script(var_start + 2, varlen - 2, FALSE) |
| 4793 | : lookup_script(var_start, varlen, TRUE)) == OK; |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4794 | imported_T *import = |
| 4795 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4796 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4797 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4798 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4799 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4800 | |
| 4801 | if (is_decl) |
| 4802 | { |
| 4803 | if (script_namespace) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4804 | semsg(_(e_cannot_declare_script_variable_in_function), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4805 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4806 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4807 | semsg(_(e_variable_already_declared_in_script), |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4808 | name); |
| 4809 | goto theend; |
| 4810 | } |
| 4811 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4812 | == SCRIPT_VERSION_VIM9 |
| 4813 | && script_namespace |
| 4814 | && !script_var && import == NULL) |
| 4815 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4816 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4817 | goto theend; |
| 4818 | } |
| 4819 | |
| 4820 | dest = dest_script; |
| 4821 | |
| 4822 | // existing script-local variables should have a type |
| 4823 | scriptvar_sid = current_sctx.sc_sid; |
| 4824 | if (import != NULL) |
| 4825 | scriptvar_sid = import->imp_sid; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4826 | if (SCRIPT_ID_VALID(scriptvar_sid)) |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4827 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4828 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4829 | rawname, TRUE); |
| 4830 | if (scriptvar_idx > 0) |
| 4831 | { |
| 4832 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4833 | svar_T *sv = |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4834 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4835 | + scriptvar_idx; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4836 | type = sv->sv_type; |
| 4837 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | else if (name[1] == ':' && name[2] != NUL) |
| 4841 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4842 | semsg(_(e_cannot_use_namespaced_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4843 | goto theend; |
| 4844 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4845 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4846 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4847 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4848 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4849 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4850 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4851 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4852 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4853 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4854 | |
| 4855 | if (declare_error) |
| 4856 | { |
| 4857 | vim9_declare_error(name); |
| 4858 | goto theend; |
| 4859 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | // handle "a:name" as a name, not index "name" on "a" |
| 4863 | if (varlen > 1 || var_start[varlen] != ':') |
| 4864 | p = var_end; |
| 4865 | |
| 4866 | if (dest != dest_option) |
| 4867 | { |
| 4868 | if (is_decl && *p == ':') |
| 4869 | { |
| 4870 | // parse optional type: "let var: type = expr" |
| 4871 | if (!VIM_ISWHITE(p[1])) |
| 4872 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4873 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4874 | goto theend; |
| 4875 | } |
| 4876 | p = skipwhite(p + 1); |
| 4877 | type = parse_type(&p, cctx->ctx_type_list); |
| 4878 | has_type = TRUE; |
| 4879 | } |
| 4880 | else if (lvar != NULL) |
| 4881 | type = lvar->lv_type; |
| 4882 | } |
| 4883 | |
| 4884 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4885 | && type->tt_type != VAR_STRING |
| 4886 | && type->tt_type != VAR_ANY) |
| 4887 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4888 | emsg(_(e_can_only_concatenate_to_string)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4889 | goto theend; |
| 4890 | } |
| 4891 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4892 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4893 | { |
| 4894 | if (oplen > 1 && !heredoc) |
| 4895 | { |
| 4896 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4897 | semsg(_(e_cannot_use_operator_on_new_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4898 | goto theend; |
| 4899 | } |
| 4900 | |
| 4901 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4902 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4903 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4904 | goto theend; |
| 4905 | lvar = reserve_local(cctx, var_start, varlen, |
| 4906 | cmdidx == CMD_const, type); |
| 4907 | if (lvar == NULL) |
| 4908 | goto theend; |
| 4909 | new_local = TRUE; |
| 4910 | } |
| 4911 | |
| 4912 | member_type = type; |
| 4913 | if (var_end > var_start + varlen) |
| 4914 | { |
| 4915 | // Something follows after the variable: "var[idx]". |
| 4916 | if (is_decl) |
| 4917 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4918 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4919 | goto theend; |
| 4920 | } |
| 4921 | |
| 4922 | if (var_start[varlen] == '[') |
| 4923 | { |
| 4924 | has_index = TRUE; |
| 4925 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4926 | member_type = &t_any; |
| 4927 | else |
| 4928 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4929 | } |
| 4930 | else |
| 4931 | { |
| 4932 | semsg("Not supported yet: %s", var_start); |
| 4933 | goto theend; |
| 4934 | } |
| 4935 | } |
| 4936 | else if (lvar == &arg_lvar) |
| 4937 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4938 | semsg(_(e_cannot_assign_to_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4939 | goto theend; |
| 4940 | } |
| 4941 | |
| 4942 | if (!heredoc) |
| 4943 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4944 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4945 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4946 | if (oplen > 0 && var_count == 0) |
| 4947 | { |
| 4948 | // skip over the "=" and the expression |
| 4949 | p = skipwhite(op + oplen); |
| 4950 | compile_expr0(&p, cctx); |
| 4951 | } |
| 4952 | } |
| 4953 | else if (oplen > 0) |
| 4954 | { |
| 4955 | type_T *stacktype; |
| 4956 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4957 | // For "var = expr" evaluate the expression. |
| 4958 | if (var_count == 0) |
| 4959 | { |
| 4960 | int r; |
| 4961 | |
| 4962 | // for "+=", "*=", "..=" etc. first load the current value |
| 4963 | if (*op != '=') |
| 4964 | { |
| 4965 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4966 | |
| 4967 | if (has_index) |
| 4968 | { |
| 4969 | // TODO: get member from list or dict |
| 4970 | emsg("Index with operation not supported yet"); |
| 4971 | goto theend; |
| 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | // Compile the expression. Temporarily hide the new local |
| 4976 | // variable here, it is not available to this expression. |
| 4977 | if (new_local) |
| 4978 | --cctx->ctx_locals.ga_len; |
| 4979 | instr_count = instr->ga_len; |
| 4980 | p = skipwhite(op + oplen); |
| 4981 | r = compile_expr0(&p, cctx); |
| 4982 | if (new_local) |
| 4983 | ++cctx->ctx_locals.ga_len; |
| 4984 | if (r == FAIL) |
| 4985 | goto theend; |
| 4986 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4987 | else if (semicolon && var_idx == var_count - 1) |
| 4988 | { |
| 4989 | // For "[var; var] = expr" get the rest of the list |
| 4990 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 4991 | goto theend; |
| 4992 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4993 | else |
| 4994 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4995 | // For "[var, var] = expr" get the "var_idx" item from the |
| 4996 | // list. |
| 4997 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 4998 | return FAIL; |
| 4999 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5000 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5001 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5002 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5003 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5004 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5005 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5006 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5007 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5008 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5009 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5010 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5011 | } |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 5012 | else if ((stacktype->tt_type == VAR_FUNC |
| 5013 | || stacktype->tt_type == VAR_PARTIAL) |
| 5014 | && var_wrong_func_name(name, TRUE)) |
| 5015 | { |
| 5016 | goto theend; |
| 5017 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5018 | else |
| 5019 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5020 | // An empty list or dict has a &t_void member, |
| 5021 | // for a variable that implies &t_any. |
| 5022 | if (stacktype == &t_list_empty) |
| 5023 | lvar->lv_type = &t_list_any; |
| 5024 | else if (stacktype == &t_dict_empty) |
| 5025 | lvar->lv_type = &t_dict_any; |
| 5026 | else |
| 5027 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5028 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5029 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5030 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5031 | { |
| 5032 | type_T *use_type = lvar->lv_type; |
| 5033 | |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5034 | // without operator type is here, otherwise below |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5035 | if (has_index) |
| 5036 | { |
| 5037 | use_type = use_type->tt_member; |
| 5038 | if (use_type == NULL) |
| 5039 | use_type = &t_void; |
| 5040 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5041 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5042 | == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5043 | goto theend; |
| 5044 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5045 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5046 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5047 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5048 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5049 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5050 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5051 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5052 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5053 | goto theend; |
| 5054 | } |
| 5055 | else if (!has_type || dest == dest_option) |
| 5056 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5057 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5058 | goto theend; |
| 5059 | } |
| 5060 | else |
| 5061 | { |
| 5062 | // variables are always initialized |
| 5063 | if (ga_grow(instr, 1) == FAIL) |
| 5064 | goto theend; |
| 5065 | switch (member_type->tt_type) |
| 5066 | { |
| 5067 | case VAR_BOOL: |
| 5068 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5069 | break; |
| 5070 | case VAR_FLOAT: |
| 5071 | #ifdef FEAT_FLOAT |
| 5072 | generate_PUSHF(cctx, 0.0); |
| 5073 | #endif |
| 5074 | break; |
| 5075 | case VAR_STRING: |
| 5076 | generate_PUSHS(cctx, NULL); |
| 5077 | break; |
| 5078 | case VAR_BLOB: |
| 5079 | generate_PUSHBLOB(cctx, NULL); |
| 5080 | break; |
| 5081 | case VAR_FUNC: |
| 5082 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5083 | break; |
| 5084 | case VAR_LIST: |
| 5085 | generate_NEWLIST(cctx, 0); |
| 5086 | break; |
| 5087 | case VAR_DICT: |
| 5088 | generate_NEWDICT(cctx, 0); |
| 5089 | break; |
| 5090 | case VAR_JOB: |
| 5091 | generate_PUSHJOB(cctx, NULL); |
| 5092 | break; |
| 5093 | case VAR_CHANNEL: |
| 5094 | generate_PUSHCHANNEL(cctx, NULL); |
| 5095 | break; |
| 5096 | case VAR_NUMBER: |
| 5097 | case VAR_UNKNOWN: |
| 5098 | case VAR_ANY: |
| 5099 | case VAR_PARTIAL: |
| 5100 | case VAR_VOID: |
| 5101 | case VAR_SPECIAL: // cannot happen |
| 5102 | generate_PUSHNR(cctx, 0); |
| 5103 | break; |
| 5104 | } |
| 5105 | } |
| 5106 | if (var_count == 0) |
| 5107 | end = p; |
| 5108 | } |
| 5109 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5110 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5111 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5112 | break; |
| 5113 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5114 | if (oplen > 0 && *op != '=') |
| 5115 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5116 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5117 | type_T *stacktype; |
| 5118 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5119 | if (*op == '.') |
| 5120 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5121 | else |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5122 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5123 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5124 | if ( |
| 5125 | #ifdef FEAT_FLOAT |
| 5126 | // If variable is float operation with number is OK. |
| 5127 | !(expected == &t_float && stacktype == &t_number) && |
| 5128 | #endif |
| 5129 | need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5130 | goto theend; |
| 5131 | |
| 5132 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5133 | { |
| 5134 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 5135 | goto theend; |
| 5136 | } |
| 5137 | else if (*op == '+') |
| 5138 | { |
| 5139 | if (generate_add_instr(cctx, |
| 5140 | operator_type(member_type, stacktype), |
| 5141 | member_type, stacktype) == FAIL) |
| 5142 | goto theend; |
| 5143 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5144 | else if (generate_two_op(cctx, op) == FAIL) |
| 5145 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5146 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5147 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5148 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5149 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5150 | int r; |
| 5151 | |
| 5152 | // Compile the "idx" in "var[idx]". |
| 5153 | if (new_local) |
| 5154 | --cctx->ctx_locals.ga_len; |
| 5155 | p = skipwhite(var_start + varlen + 1); |
| 5156 | r = compile_expr0(&p, cctx); |
| 5157 | if (new_local) |
| 5158 | ++cctx->ctx_locals.ga_len; |
| 5159 | if (r == FAIL) |
| 5160 | goto theend; |
| 5161 | if (*skipwhite(p) != ']') |
| 5162 | { |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 5163 | // this should not happen |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5164 | emsg(_(e_missbrac)); |
| 5165 | goto theend; |
| 5166 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5167 | if (type == &t_any) |
| 5168 | { |
| 5169 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5170 | stack->ga_len - 1]; |
| 5171 | // Index on variable of unknown type: guess the type from the |
| 5172 | // index type: number is dict, otherwise dict. |
| 5173 | // TODO: should do the assignment at runtime |
| 5174 | if (idx_type->tt_type == VAR_NUMBER) |
| 5175 | type = &t_list_any; |
| 5176 | else |
| 5177 | type = &t_dict_any; |
| 5178 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5179 | if (type->tt_type == VAR_DICT |
| 5180 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5181 | goto theend; |
| 5182 | if (type->tt_type == VAR_LIST |
| 5183 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5184 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5185 | { |
| 5186 | emsg(_(e_number_exp)); |
| 5187 | goto theend; |
| 5188 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5189 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5190 | // Load the dict or list. On the stack we then have: |
| 5191 | // - value |
| 5192 | // - index |
| 5193 | // - variable |
| 5194 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5195 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5196 | if (type->tt_type == VAR_LIST) |
| 5197 | { |
| 5198 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5199 | return FAIL; |
| 5200 | } |
| 5201 | else if (type->tt_type == VAR_DICT) |
| 5202 | { |
| 5203 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5204 | return FAIL; |
| 5205 | } |
| 5206 | else |
| 5207 | { |
| 5208 | emsg(_(e_listreq)); |
| 5209 | goto theend; |
| 5210 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5211 | } |
| 5212 | else |
| 5213 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5214 | switch (dest) |
| 5215 | { |
| 5216 | case dest_option: |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 5217 | generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5218 | opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5219 | break; |
| 5220 | case dest_global: |
| 5221 | // include g: with the name, easier to execute that way |
| 5222 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5223 | break; |
| 5224 | case dest_buffer: |
| 5225 | // include b: with the name, easier to execute that way |
| 5226 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5227 | break; |
| 5228 | case dest_window: |
| 5229 | // include w: with the name, easier to execute that way |
| 5230 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5231 | break; |
| 5232 | case dest_tab: |
| 5233 | // include t: with the name, easier to execute that way |
| 5234 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5235 | break; |
| 5236 | case dest_env: |
| 5237 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5238 | break; |
| 5239 | case dest_reg: |
| 5240 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5241 | break; |
| 5242 | case dest_vimvar: |
| 5243 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5244 | break; |
| 5245 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5246 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5247 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5248 | { |
| 5249 | char_u *name_s = name; |
| 5250 | |
| 5251 | // Include s: in the name for store_var() |
| 5252 | if (name[1] != ':') |
| 5253 | { |
| 5254 | int len = (int)STRLEN(name) + 3; |
| 5255 | |
| 5256 | name_s = alloc(len); |
| 5257 | if (name_s == NULL) |
| 5258 | name_s = name; |
| 5259 | else |
| 5260 | vim_snprintf((char *)name_s, len, |
| 5261 | "s:%s", name); |
| 5262 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5263 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5264 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5265 | if (name_s != name) |
| 5266 | vim_free(name_s); |
| 5267 | } |
| 5268 | else |
| 5269 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5270 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5271 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5272 | break; |
| 5273 | case dest_local: |
| 5274 | if (lvar != NULL) |
| 5275 | { |
| 5276 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5277 | + instr->ga_len - 1; |
| 5278 | |
| 5279 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5280 | // ISN_STORE into ISN_STORENR |
| 5281 | if (!lvar->lv_from_outer |
| 5282 | && instr->ga_len == instr_count + 1 |
| 5283 | && isn->isn_type == ISN_PUSHNR) |
| 5284 | { |
| 5285 | varnumber_T val = isn->isn_arg.number; |
| 5286 | |
| 5287 | isn->isn_type = ISN_STORENR; |
| 5288 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5289 | isn->isn_arg.storenr.stnr_val = val; |
| 5290 | if (stack->ga_len > 0) |
| 5291 | --stack->ga_len; |
| 5292 | } |
| 5293 | else if (lvar->lv_from_outer) |
| 5294 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5295 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5296 | else |
| 5297 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5298 | } |
| 5299 | break; |
| 5300 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5301 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5302 | |
| 5303 | if (var_idx + 1 < var_count) |
| 5304 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5305 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5306 | |
| 5307 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5308 | if (var_count > 0 && !semicolon) |
| 5309 | { |
| 5310 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5311 | goto theend; |
| 5312 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5313 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5314 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5315 | |
| 5316 | theend: |
| 5317 | vim_free(name); |
| 5318 | return ret; |
| 5319 | } |
| 5320 | |
| 5321 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5322 | * Check if "name" can be "unlet". |
| 5323 | */ |
| 5324 | int |
| 5325 | check_vim9_unlet(char_u *name) |
| 5326 | { |
| 5327 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5328 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 5329 | // "unlet s:var" is allowed in legacy script. |
| 5330 | if (*name == 's' && !script_is_vim9()) |
| 5331 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5332 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5333 | return FAIL; |
| 5334 | } |
| 5335 | return OK; |
| 5336 | } |
| 5337 | |
| 5338 | /* |
| 5339 | * Callback passed to ex_unletlock(). |
| 5340 | */ |
| 5341 | static int |
| 5342 | compile_unlet( |
| 5343 | lval_T *lvp, |
| 5344 | char_u *name_end, |
| 5345 | exarg_T *eap, |
| 5346 | int deep UNUSED, |
| 5347 | void *coookie) |
| 5348 | { |
| 5349 | cctx_T *cctx = coookie; |
| 5350 | |
| 5351 | if (lvp->ll_tv == NULL) |
| 5352 | { |
| 5353 | char_u *p = lvp->ll_name; |
| 5354 | int cc = *name_end; |
| 5355 | int ret = OK; |
| 5356 | |
| 5357 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5358 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5359 | if (*p == '$') |
| 5360 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5361 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5362 | ret = FAIL; |
| 5363 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5364 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5365 | |
| 5366 | *name_end = cc; |
| 5367 | return ret; |
| 5368 | } |
| 5369 | |
| 5370 | // TODO: unlet {list}[idx] |
| 5371 | // TODO: unlet {dict}[key] |
| 5372 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5373 | return FAIL; |
| 5374 | } |
| 5375 | |
| 5376 | /* |
| 5377 | * compile "unlet var", "lock var" and "unlock var" |
| 5378 | * "arg" points to "var". |
| 5379 | */ |
| 5380 | static char_u * |
| 5381 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5382 | { |
| 5383 | char_u *p = arg; |
| 5384 | |
| 5385 | if (eap->cmdidx != CMD_unlet) |
| 5386 | { |
| 5387 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5388 | return NULL; |
| 5389 | } |
| 5390 | |
| 5391 | if (*p == '!') |
| 5392 | { |
| 5393 | p = skipwhite(p + 1); |
| 5394 | eap->forceit = TRUE; |
| 5395 | } |
| 5396 | |
| 5397 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5398 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5399 | } |
| 5400 | |
| 5401 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5402 | * Compile an :import command. |
| 5403 | */ |
| 5404 | static char_u * |
| 5405 | compile_import(char_u *arg, cctx_T *cctx) |
| 5406 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5407 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /* |
| 5411 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5412 | */ |
| 5413 | static int |
| 5414 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5415 | { |
| 5416 | garray_T *instr = &cctx->ctx_instr; |
| 5417 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5418 | |
| 5419 | if (endlabel == NULL) |
| 5420 | return FAIL; |
| 5421 | endlabel->el_next = *el; |
| 5422 | *el = endlabel; |
| 5423 | endlabel->el_end_label = instr->ga_len; |
| 5424 | |
| 5425 | generate_JUMP(cctx, when, 0); |
| 5426 | return OK; |
| 5427 | } |
| 5428 | |
| 5429 | static void |
| 5430 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5431 | { |
| 5432 | garray_T *instr = &cctx->ctx_instr; |
| 5433 | |
| 5434 | while (*el != NULL) |
| 5435 | { |
| 5436 | endlabel_T *cur = (*el); |
| 5437 | isn_T *isn; |
| 5438 | |
| 5439 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5440 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5441 | *el = cur->el_next; |
| 5442 | vim_free(cur); |
| 5443 | } |
| 5444 | } |
| 5445 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5446 | static void |
| 5447 | compile_free_jump_to_end(endlabel_T **el) |
| 5448 | { |
| 5449 | while (*el != NULL) |
| 5450 | { |
| 5451 | endlabel_T *cur = (*el); |
| 5452 | |
| 5453 | *el = cur->el_next; |
| 5454 | vim_free(cur); |
| 5455 | } |
| 5456 | } |
| 5457 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5458 | /* |
| 5459 | * Create a new scope and set up the generic items. |
| 5460 | */ |
| 5461 | static scope_T * |
| 5462 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5463 | { |
| 5464 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5465 | |
| 5466 | if (scope == NULL) |
| 5467 | return NULL; |
| 5468 | scope->se_outer = cctx->ctx_scope; |
| 5469 | cctx->ctx_scope = scope; |
| 5470 | scope->se_type = type; |
| 5471 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5472 | return scope; |
| 5473 | } |
| 5474 | |
| 5475 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5476 | * Free the current scope and go back to the outer scope. |
| 5477 | */ |
| 5478 | static void |
| 5479 | drop_scope(cctx_T *cctx) |
| 5480 | { |
| 5481 | scope_T *scope = cctx->ctx_scope; |
| 5482 | |
| 5483 | if (scope == NULL) |
| 5484 | { |
| 5485 | iemsg("calling drop_scope() without a scope"); |
| 5486 | return; |
| 5487 | } |
| 5488 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5489 | switch (scope->se_type) |
| 5490 | { |
| 5491 | case IF_SCOPE: |
| 5492 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5493 | case FOR_SCOPE: |
| 5494 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5495 | case WHILE_SCOPE: |
| 5496 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5497 | case TRY_SCOPE: |
| 5498 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5499 | case NO_SCOPE: |
| 5500 | case BLOCK_SCOPE: |
| 5501 | break; |
| 5502 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5503 | vim_free(scope); |
| 5504 | } |
| 5505 | |
| 5506 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5507 | * compile "if expr" |
| 5508 | * |
| 5509 | * "if expr" Produces instructions: |
| 5510 | * EVAL expr Push result of "expr" |
| 5511 | * JUMP_IF_FALSE end |
| 5512 | * ... body ... |
| 5513 | * end: |
| 5514 | * |
| 5515 | * "if expr | else" Produces instructions: |
| 5516 | * EVAL expr Push result of "expr" |
| 5517 | * JUMP_IF_FALSE else |
| 5518 | * ... body ... |
| 5519 | * JUMP_ALWAYS end |
| 5520 | * else: |
| 5521 | * ... body ... |
| 5522 | * end: |
| 5523 | * |
| 5524 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5525 | * EVAL expr Push result of "expr" |
| 5526 | * JUMP_IF_FALSE elseif |
| 5527 | * ... body ... |
| 5528 | * JUMP_ALWAYS end |
| 5529 | * elseif: |
| 5530 | * EVAL expr Push result of "expr" |
| 5531 | * JUMP_IF_FALSE else |
| 5532 | * ... body ... |
| 5533 | * JUMP_ALWAYS end |
| 5534 | * else: |
| 5535 | * ... body ... |
| 5536 | * end: |
| 5537 | */ |
| 5538 | static char_u * |
| 5539 | compile_if(char_u *arg, cctx_T *cctx) |
| 5540 | { |
| 5541 | char_u *p = arg; |
| 5542 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5543 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5544 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5545 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5546 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5547 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5548 | CLEAR_FIELD(ppconst); |
| 5549 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5550 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5551 | clear_ppconst(&ppconst); |
| 5552 | return NULL; |
| 5553 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5554 | if (cctx->ctx_skip == SKIP_YES) |
| 5555 | clear_ppconst(&ppconst); |
| 5556 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5557 | { |
| 5558 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5559 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5560 | clear_ppconst(&ppconst); |
| 5561 | } |
| 5562 | else |
| 5563 | { |
| 5564 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5565 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5566 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5567 | return NULL; |
| 5568 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5569 | |
| 5570 | scope = new_scope(cctx, IF_SCOPE); |
| 5571 | if (scope == NULL) |
| 5572 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5573 | scope->se_skip_save = skip_save; |
| 5574 | // "is_had_return" will be reset if any block does not end in :return |
| 5575 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5576 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5577 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5578 | { |
| 5579 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5580 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5581 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5582 | } |
| 5583 | else |
| 5584 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5585 | |
| 5586 | return p; |
| 5587 | } |
| 5588 | |
| 5589 | static char_u * |
| 5590 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5591 | { |
| 5592 | char_u *p = arg; |
| 5593 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5594 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5595 | isn_T *isn; |
| 5596 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5597 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5598 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5599 | |
| 5600 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5601 | { |
| 5602 | emsg(_(e_elseif_without_if)); |
| 5603 | return NULL; |
| 5604 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5605 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5606 | if (!cctx->ctx_had_return) |
| 5607 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5608 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5609 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5610 | { |
| 5611 | 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] | 5612 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5613 | return NULL; |
| 5614 | // previous "if" or "elseif" jumps here |
| 5615 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5616 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5617 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5618 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5619 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5620 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5621 | if (cctx->ctx_skip == SKIP_YES) |
| 5622 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5623 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5624 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5625 | clear_ppconst(&ppconst); |
| 5626 | return NULL; |
| 5627 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5628 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5629 | if (scope->se_skip_save == SKIP_YES) |
| 5630 | clear_ppconst(&ppconst); |
| 5631 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5632 | { |
| 5633 | // The expression results in a constant. |
| 5634 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5635 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5636 | clear_ppconst(&ppconst); |
| 5637 | scope->se_u.se_if.is_if_label = -1; |
| 5638 | } |
| 5639 | else |
| 5640 | { |
| 5641 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5642 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5643 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5644 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5645 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5646 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5647 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5648 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5649 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5650 | |
| 5651 | return p; |
| 5652 | } |
| 5653 | |
| 5654 | static char_u * |
| 5655 | compile_else(char_u *arg, cctx_T *cctx) |
| 5656 | { |
| 5657 | char_u *p = arg; |
| 5658 | garray_T *instr = &cctx->ctx_instr; |
| 5659 | isn_T *isn; |
| 5660 | scope_T *scope = cctx->ctx_scope; |
| 5661 | |
| 5662 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5663 | { |
| 5664 | emsg(_(e_else_without_if)); |
| 5665 | return NULL; |
| 5666 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5667 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5668 | if (!cctx->ctx_had_return) |
| 5669 | scope->se_u.se_if.is_had_return = FALSE; |
| 5670 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5671 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5672 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5673 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5674 | // 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] | 5675 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5676 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5677 | if (!cctx->ctx_had_return |
| 5678 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5679 | JUMP_ALWAYS, cctx) == FAIL) |
| 5680 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5681 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5682 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5683 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5684 | { |
| 5685 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5686 | { |
| 5687 | // previous "if" or "elseif" jumps here |
| 5688 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5689 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5690 | scope->se_u.se_if.is_if_label = -1; |
| 5691 | } |
| 5692 | } |
| 5693 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5694 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5695 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5696 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5697 | |
| 5698 | return p; |
| 5699 | } |
| 5700 | |
| 5701 | static char_u * |
| 5702 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5703 | { |
| 5704 | scope_T *scope = cctx->ctx_scope; |
| 5705 | ifscope_T *ifscope; |
| 5706 | garray_T *instr = &cctx->ctx_instr; |
| 5707 | isn_T *isn; |
| 5708 | |
| 5709 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5710 | { |
| 5711 | emsg(_(e_endif_without_if)); |
| 5712 | return NULL; |
| 5713 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5714 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5715 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5716 | if (!cctx->ctx_had_return) |
| 5717 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5718 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5719 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5720 | { |
| 5721 | // previous "if" or "elseif" jumps here |
| 5722 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5723 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5724 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5725 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5726 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5727 | cctx->ctx_skip = scope->se_skip_save; |
| 5728 | |
| 5729 | // If all the blocks end in :return and there is an :else then the |
| 5730 | // had_return flag is set. |
| 5731 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5732 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5733 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5734 | return arg; |
| 5735 | } |
| 5736 | |
| 5737 | /* |
| 5738 | * compile "for var in expr" |
| 5739 | * |
| 5740 | * Produces instructions: |
| 5741 | * PUSHNR -1 |
| 5742 | * STORE loop-idx Set index to -1 |
| 5743 | * EVAL expr Push result of "expr" |
| 5744 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5745 | * - if beyond end, jump to "end" |
| 5746 | * - otherwise get item from list and push it |
| 5747 | * STORE var Store item in "var" |
| 5748 | * ... body ... |
| 5749 | * JUMP top Jump back to repeat |
| 5750 | * end: DROP Drop the result of "expr" |
| 5751 | * |
| 5752 | */ |
| 5753 | static char_u * |
| 5754 | compile_for(char_u *arg, cctx_T *cctx) |
| 5755 | { |
| 5756 | char_u *p; |
| 5757 | size_t varlen; |
| 5758 | garray_T *instr = &cctx->ctx_instr; |
| 5759 | garray_T *stack = &cctx->ctx_type_stack; |
| 5760 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5761 | lvar_T *loop_lvar; // loop iteration variable |
| 5762 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5763 | type_T *vartype; |
| 5764 | |
| 5765 | // TODO: list of variables: "for [key, value] in dict" |
| 5766 | // parse "var" |
| 5767 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5768 | ; |
| 5769 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5770 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5771 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5772 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5773 | semsg(_(e_variable_already_declared), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5774 | return NULL; |
| 5775 | } |
| 5776 | |
| 5777 | // consume "in" |
| 5778 | p = skipwhite(p); |
| 5779 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5780 | { |
| 5781 | emsg(_(e_missing_in)); |
| 5782 | return NULL; |
| 5783 | } |
| 5784 | p = skipwhite(p + 2); |
| 5785 | |
| 5786 | |
| 5787 | scope = new_scope(cctx, FOR_SCOPE); |
| 5788 | if (scope == NULL) |
| 5789 | return NULL; |
| 5790 | |
| 5791 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5792 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5793 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5794 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5795 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5796 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5797 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5798 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5799 | |
| 5800 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5801 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5802 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5803 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5804 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5805 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5806 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5807 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5808 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5809 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5810 | |
| 5811 | // compile "expr", it remains on the stack until "endfor" |
| 5812 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5813 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5814 | { |
| 5815 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5816 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5817 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5818 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5819 | // Now that we know the type of "var", check that it is a list, now or at |
| 5820 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5821 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5822 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5823 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5824 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5825 | return NULL; |
| 5826 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5827 | 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] | 5828 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5829 | |
| 5830 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5831 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5832 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5833 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5834 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5835 | |
| 5836 | return arg; |
| 5837 | } |
| 5838 | |
| 5839 | /* |
| 5840 | * compile "endfor" |
| 5841 | */ |
| 5842 | static char_u * |
| 5843 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5844 | { |
| 5845 | garray_T *instr = &cctx->ctx_instr; |
| 5846 | scope_T *scope = cctx->ctx_scope; |
| 5847 | forscope_T *forscope; |
| 5848 | isn_T *isn; |
| 5849 | |
| 5850 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5851 | { |
| 5852 | emsg(_(e_for)); |
| 5853 | return NULL; |
| 5854 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5855 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5856 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5857 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5858 | |
| 5859 | // At end of ":for" scope jump back to the FOR instruction. |
| 5860 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5861 | |
| 5862 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5863 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5864 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5865 | |
| 5866 | // Fill in the "end" label any BREAK statements |
| 5867 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5868 | |
| 5869 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5870 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5871 | return NULL; |
| 5872 | |
| 5873 | vim_free(scope); |
| 5874 | |
| 5875 | return arg; |
| 5876 | } |
| 5877 | |
| 5878 | /* |
| 5879 | * compile "while expr" |
| 5880 | * |
| 5881 | * Produces instructions: |
| 5882 | * top: EVAL expr Push result of "expr" |
| 5883 | * JUMP_IF_FALSE end jump if false |
| 5884 | * ... body ... |
| 5885 | * JUMP top Jump back to repeat |
| 5886 | * end: |
| 5887 | * |
| 5888 | */ |
| 5889 | static char_u * |
| 5890 | compile_while(char_u *arg, cctx_T *cctx) |
| 5891 | { |
| 5892 | char_u *p = arg; |
| 5893 | garray_T *instr = &cctx->ctx_instr; |
| 5894 | scope_T *scope; |
| 5895 | |
| 5896 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5897 | if (scope == NULL) |
| 5898 | return NULL; |
| 5899 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5900 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5901 | |
| 5902 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5903 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5904 | return NULL; |
| 5905 | |
| 5906 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5907 | 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] | 5908 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5909 | return FAIL; |
| 5910 | |
| 5911 | return p; |
| 5912 | } |
| 5913 | |
| 5914 | /* |
| 5915 | * compile "endwhile" |
| 5916 | */ |
| 5917 | static char_u * |
| 5918 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5919 | { |
| 5920 | scope_T *scope = cctx->ctx_scope; |
| 5921 | |
| 5922 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5923 | { |
| 5924 | emsg(_(e_while)); |
| 5925 | return NULL; |
| 5926 | } |
| 5927 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5928 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5929 | |
| 5930 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5931 | 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] | 5932 | |
| 5933 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5934 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5935 | 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] | 5936 | |
| 5937 | vim_free(scope); |
| 5938 | |
| 5939 | return arg; |
| 5940 | } |
| 5941 | |
| 5942 | /* |
| 5943 | * compile "continue" |
| 5944 | */ |
| 5945 | static char_u * |
| 5946 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5947 | { |
| 5948 | scope_T *scope = cctx->ctx_scope; |
| 5949 | |
| 5950 | for (;;) |
| 5951 | { |
| 5952 | if (scope == NULL) |
| 5953 | { |
| 5954 | emsg(_(e_continue)); |
| 5955 | return NULL; |
| 5956 | } |
| 5957 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5958 | break; |
| 5959 | scope = scope->se_outer; |
| 5960 | } |
| 5961 | |
| 5962 | // Jump back to the FOR or WHILE instruction. |
| 5963 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5964 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5965 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5966 | return arg; |
| 5967 | } |
| 5968 | |
| 5969 | /* |
| 5970 | * compile "break" |
| 5971 | */ |
| 5972 | static char_u * |
| 5973 | compile_break(char_u *arg, cctx_T *cctx) |
| 5974 | { |
| 5975 | scope_T *scope = cctx->ctx_scope; |
| 5976 | endlabel_T **el; |
| 5977 | |
| 5978 | for (;;) |
| 5979 | { |
| 5980 | if (scope == NULL) |
| 5981 | { |
| 5982 | emsg(_(e_break)); |
| 5983 | return NULL; |
| 5984 | } |
| 5985 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5986 | break; |
| 5987 | scope = scope->se_outer; |
| 5988 | } |
| 5989 | |
| 5990 | // Jump to the end of the FOR or WHILE loop. |
| 5991 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5992 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5993 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5994 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5995 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5996 | return FAIL; |
| 5997 | |
| 5998 | return arg; |
| 5999 | } |
| 6000 | |
| 6001 | /* |
| 6002 | * compile "{" start of block |
| 6003 | */ |
| 6004 | static char_u * |
| 6005 | compile_block(char_u *arg, cctx_T *cctx) |
| 6006 | { |
| 6007 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6008 | return NULL; |
| 6009 | return skipwhite(arg + 1); |
| 6010 | } |
| 6011 | |
| 6012 | /* |
| 6013 | * compile end of block: drop one scope |
| 6014 | */ |
| 6015 | static void |
| 6016 | compile_endblock(cctx_T *cctx) |
| 6017 | { |
| 6018 | scope_T *scope = cctx->ctx_scope; |
| 6019 | |
| 6020 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6021 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6022 | vim_free(scope); |
| 6023 | } |
| 6024 | |
| 6025 | /* |
| 6026 | * compile "try" |
| 6027 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6028 | * finally. |
| 6029 | * Creates another scope for the "try" block itself. |
| 6030 | * TRY instruction sets up exception handling at runtime. |
| 6031 | * |
| 6032 | * "try" |
| 6033 | * TRY -> catch1, -> finally push trystack entry |
| 6034 | * ... try block |
| 6035 | * "throw {exception}" |
| 6036 | * EVAL {exception} |
| 6037 | * THROW create exception |
| 6038 | * ... try block |
| 6039 | * " catch {expr}" |
| 6040 | * JUMP -> finally |
| 6041 | * catch1: PUSH exeception |
| 6042 | * EVAL {expr} |
| 6043 | * MATCH |
| 6044 | * JUMP nomatch -> catch2 |
| 6045 | * CATCH remove exception |
| 6046 | * ... catch block |
| 6047 | * " catch" |
| 6048 | * JUMP -> finally |
| 6049 | * catch2: CATCH remove exception |
| 6050 | * ... catch block |
| 6051 | * " finally" |
| 6052 | * finally: |
| 6053 | * ... finally block |
| 6054 | * " endtry" |
| 6055 | * ENDTRY pop trystack entry, may rethrow |
| 6056 | */ |
| 6057 | static char_u * |
| 6058 | compile_try(char_u *arg, cctx_T *cctx) |
| 6059 | { |
| 6060 | garray_T *instr = &cctx->ctx_instr; |
| 6061 | scope_T *try_scope; |
| 6062 | scope_T *scope; |
| 6063 | |
| 6064 | // scope that holds the jumps that go to catch/finally/endtry |
| 6065 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6066 | if (try_scope == NULL) |
| 6067 | return NULL; |
| 6068 | |
| 6069 | // "catch" is set when the first ":catch" is found. |
| 6070 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6071 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6072 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6073 | return NULL; |
| 6074 | |
| 6075 | // scope for the try block itself |
| 6076 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6077 | if (scope == NULL) |
| 6078 | return NULL; |
| 6079 | |
| 6080 | return arg; |
| 6081 | } |
| 6082 | |
| 6083 | /* |
| 6084 | * compile "catch {expr}" |
| 6085 | */ |
| 6086 | static char_u * |
| 6087 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6088 | { |
| 6089 | scope_T *scope = cctx->ctx_scope; |
| 6090 | garray_T *instr = &cctx->ctx_instr; |
| 6091 | char_u *p; |
| 6092 | isn_T *isn; |
| 6093 | |
| 6094 | // end block scope from :try or :catch |
| 6095 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6096 | compile_endblock(cctx); |
| 6097 | scope = cctx->ctx_scope; |
| 6098 | |
| 6099 | // Error if not in a :try scope |
| 6100 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6101 | { |
| 6102 | emsg(_(e_catch)); |
| 6103 | return NULL; |
| 6104 | } |
| 6105 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6106 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6107 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6108 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6109 | return NULL; |
| 6110 | } |
| 6111 | |
| 6112 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6113 | 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] | 6114 | JUMP_ALWAYS, cctx) == FAIL) |
| 6115 | return NULL; |
| 6116 | |
| 6117 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6118 | 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] | 6119 | if (isn->isn_arg.try.try_catch == 0) |
| 6120 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6121 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6122 | { |
| 6123 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6124 | 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] | 6125 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6126 | } |
| 6127 | |
| 6128 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6129 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6130 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6131 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6132 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6133 | } |
| 6134 | else |
| 6135 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6136 | char_u *end; |
| 6137 | char_u *pat; |
| 6138 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6139 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6140 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6142 | // Push v:exception, push {expr} and MATCH |
| 6143 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6144 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6145 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6146 | if (*end != *p) |
| 6147 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 6148 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6149 | vim_free(tofree); |
| 6150 | return FAIL; |
| 6151 | } |
| 6152 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6153 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6154 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6155 | len = (int)(end - tofree); |
| 6156 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6157 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6158 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6159 | if (pat == NULL) |
| 6160 | return FAIL; |
| 6161 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6162 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6163 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6164 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6165 | return NULL; |
| 6166 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6167 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6168 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6169 | return NULL; |
| 6170 | } |
| 6171 | |
| 6172 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6173 | return NULL; |
| 6174 | |
| 6175 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6176 | return NULL; |
| 6177 | return p; |
| 6178 | } |
| 6179 | |
| 6180 | static char_u * |
| 6181 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6182 | { |
| 6183 | scope_T *scope = cctx->ctx_scope; |
| 6184 | garray_T *instr = &cctx->ctx_instr; |
| 6185 | isn_T *isn; |
| 6186 | |
| 6187 | // end block scope from :try or :catch |
| 6188 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6189 | compile_endblock(cctx); |
| 6190 | scope = cctx->ctx_scope; |
| 6191 | |
| 6192 | // Error if not in a :try scope |
| 6193 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6194 | { |
| 6195 | emsg(_(e_finally)); |
| 6196 | return NULL; |
| 6197 | } |
| 6198 | |
| 6199 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6200 | 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] | 6201 | if (isn->isn_arg.try.try_finally != 0) |
| 6202 | { |
| 6203 | emsg(_(e_finally_dup)); |
| 6204 | return NULL; |
| 6205 | } |
| 6206 | |
| 6207 | // 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] | 6208 | 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] | 6209 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6210 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6211 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6212 | { |
| 6213 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6214 | 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] | 6215 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6216 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6219 | // TODO: set index in ts_finally_label jumps |
| 6220 | |
| 6221 | return arg; |
| 6222 | } |
| 6223 | |
| 6224 | static char_u * |
| 6225 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6226 | { |
| 6227 | scope_T *scope = cctx->ctx_scope; |
| 6228 | garray_T *instr = &cctx->ctx_instr; |
| 6229 | isn_T *isn; |
| 6230 | |
| 6231 | // end block scope from :catch or :finally |
| 6232 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6233 | compile_endblock(cctx); |
| 6234 | scope = cctx->ctx_scope; |
| 6235 | |
| 6236 | // Error if not in a :try scope |
| 6237 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6238 | { |
| 6239 | if (scope == NULL) |
| 6240 | emsg(_(e_no_endtry)); |
| 6241 | else if (scope->se_type == WHILE_SCOPE) |
| 6242 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6243 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6244 | emsg(_(e_endfor)); |
| 6245 | else |
| 6246 | emsg(_(e_endif)); |
| 6247 | return NULL; |
| 6248 | } |
| 6249 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6250 | 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] | 6251 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6252 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6253 | emsg(_(e_missing_catch_or_finally)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6254 | return NULL; |
| 6255 | } |
| 6256 | |
| 6257 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6258 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6259 | 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] | 6260 | |
| 6261 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6262 | if (isn->isn_arg.try.try_catch == 0) |
| 6263 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6264 | if (isn->isn_arg.try.try_finally == 0) |
| 6265 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6266 | |
| 6267 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6268 | { |
| 6269 | // Last catch without match jumps here |
| 6270 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6271 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6272 | } |
| 6273 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6274 | compile_endblock(cctx); |
| 6275 | |
| 6276 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6277 | return NULL; |
| 6278 | return arg; |
| 6279 | } |
| 6280 | |
| 6281 | /* |
| 6282 | * compile "throw {expr}" |
| 6283 | */ |
| 6284 | static char_u * |
| 6285 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6286 | { |
| 6287 | char_u *p = skipwhite(arg); |
| 6288 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6289 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6290 | return NULL; |
| 6291 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6292 | return NULL; |
| 6293 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6294 | return NULL; |
| 6295 | |
| 6296 | return p; |
| 6297 | } |
| 6298 | |
| 6299 | /* |
| 6300 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6301 | * compile "echomsg expr" |
| 6302 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6303 | * compile "execute expr" |
| 6304 | */ |
| 6305 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6306 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6307 | { |
| 6308 | char_u *p = arg; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6309 | char_u *prev; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6310 | int count = 0; |
| 6311 | |
| 6312 | for (;;) |
| 6313 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6314 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6315 | return NULL; |
| 6316 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6317 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6318 | p = skipwhite(p); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6319 | if (ends_excmd2(prev, p)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6320 | break; |
| 6321 | } |
| 6322 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6323 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6324 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6325 | else if (cmdidx == CMD_execute) |
| 6326 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6327 | else if (cmdidx == CMD_echomsg) |
| 6328 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6329 | else |
| 6330 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6331 | return p; |
| 6332 | } |
| 6333 | |
| 6334 | /* |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6335 | * :put r |
| 6336 | * :put ={expr} |
| 6337 | */ |
| 6338 | static char_u * |
| 6339 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 6340 | { |
| 6341 | char_u *line = arg; |
| 6342 | linenr_T lnum; |
| 6343 | char *errormsg; |
| 6344 | int above = FALSE; |
| 6345 | |
| 6346 | if (*arg == '!') |
| 6347 | { |
| 6348 | above = TRUE; |
| 6349 | line = skipwhite(arg + 1); |
| 6350 | } |
| 6351 | eap->regname = *line; |
| 6352 | |
| 6353 | if (eap->regname == '=') |
| 6354 | { |
| 6355 | char_u *p = line + 1; |
| 6356 | |
| 6357 | if (compile_expr0(&p, cctx) == FAIL) |
| 6358 | return NULL; |
| 6359 | line = p; |
| 6360 | } |
| 6361 | else if (eap->regname != NUL) |
| 6362 | ++line; |
| 6363 | |
| 6364 | // TODO: if the range is something like "$" need to evaluate at runtime |
| 6365 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
| 6366 | return NULL; |
| 6367 | if (eap->addr_count == 0) |
| 6368 | lnum = -1; |
| 6369 | else |
| 6370 | lnum = eap->line2; |
| 6371 | if (above) |
| 6372 | --lnum; |
| 6373 | |
| 6374 | generate_PUT(cctx, eap->regname, lnum); |
| 6375 | return line; |
| 6376 | } |
| 6377 | |
| 6378 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6379 | * A command that is not compiled, execute with legacy code. |
| 6380 | */ |
| 6381 | static char_u * |
| 6382 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6383 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6384 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6385 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6386 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6387 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6388 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6389 | goto theend; |
| 6390 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6391 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6392 | { |
| 6393 | long argt = excmd_get_argt(eap->cmdidx); |
| 6394 | int usefilter = FALSE; |
| 6395 | |
| 6396 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6397 | |
| 6398 | // If the command can be followed by a bar, find the bar and truncate |
| 6399 | // it, so that the following command can be compiled. |
| 6400 | // The '|' is overwritten with a NUL, it is put back below. |
| 6401 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6402 | && *eap->arg == '!') |
| 6403 | // :w !filter or :r !filter or :r! filter |
| 6404 | usefilter = TRUE; |
| 6405 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6406 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 6407 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6408 | separate_nextcmd(eap); |
| 6409 | if (eap->nextcmd != NULL) |
| 6410 | nextcmd = eap->nextcmd; |
| 6411 | } |
| 6412 | } |
| 6413 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6414 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6415 | { |
| 6416 | // expand filename in "syntax include [@group] filename" |
| 6417 | has_expr = TRUE; |
| 6418 | eap->arg = skipwhite(eap->arg + 7); |
| 6419 | if (*eap->arg == '@') |
| 6420 | eap->arg = skiptowhite(eap->arg); |
| 6421 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6422 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6423 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6424 | { |
| 6425 | int count = 0; |
| 6426 | char_u *start = skipwhite(line); |
| 6427 | |
| 6428 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6429 | // PUSHS ":cmd xxx" |
| 6430 | // eval expr1 |
| 6431 | // PUSHS "yyy" |
| 6432 | // eval expr2 |
| 6433 | // PUSHS "zzz" |
| 6434 | // EXECCONCAT 5 |
| 6435 | for (;;) |
| 6436 | { |
| 6437 | if (p > start) |
| 6438 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6439 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6440 | ++count; |
| 6441 | } |
| 6442 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6443 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6444 | return NULL; |
| 6445 | may_generate_2STRING(-1, cctx); |
| 6446 | ++count; |
| 6447 | p = skipwhite(p); |
| 6448 | if (*p != '`') |
| 6449 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6450 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6451 | return NULL; |
| 6452 | } |
| 6453 | start = p + 1; |
| 6454 | |
| 6455 | p = (char_u *)strstr((char *)start, "`="); |
| 6456 | if (p == NULL) |
| 6457 | { |
| 6458 | if (*skipwhite(start) != NUL) |
| 6459 | { |
| 6460 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6461 | ++count; |
| 6462 | } |
| 6463 | break; |
| 6464 | } |
| 6465 | } |
| 6466 | generate_EXECCONCAT(cctx, count); |
| 6467 | } |
| 6468 | else |
| 6469 | generate_EXEC(cctx, line); |
| 6470 | |
| 6471 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6472 | if (*nextcmd != NUL) |
| 6473 | { |
| 6474 | // the parser expects a pointer to the bar, put it back |
| 6475 | --nextcmd; |
| 6476 | *nextcmd = '|'; |
| 6477 | } |
| 6478 | |
| 6479 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6483 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6484 | * 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] | 6485 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6486 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6487 | add_def_function(ufunc_T *ufunc) |
| 6488 | { |
| 6489 | dfunc_T *dfunc; |
| 6490 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6491 | if (def_functions.ga_len == 0) |
| 6492 | { |
| 6493 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6494 | // wasn't set. |
| 6495 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6496 | return FAIL; |
| 6497 | ++def_functions.ga_len; |
| 6498 | } |
| 6499 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6500 | // Add the function to "def_functions". |
| 6501 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6502 | return FAIL; |
| 6503 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6504 | CLEAR_POINTER(dfunc); |
| 6505 | dfunc->df_idx = def_functions.ga_len; |
| 6506 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6507 | dfunc->df_ufunc = ufunc; |
| 6508 | ++def_functions.ga_len; |
| 6509 | return OK; |
| 6510 | } |
| 6511 | |
| 6512 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6513 | * After ex_function() has collected all the function lines: parse and compile |
| 6514 | * the lines into instructions. |
| 6515 | * Adds the function to "def_functions". |
| 6516 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6517 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6518 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6519 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6520 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6521 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6522 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6523 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6524 | 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] | 6525 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6526 | char_u *line = NULL; |
| 6527 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6528 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6529 | cctx_T cctx; |
| 6530 | garray_T *instr; |
| 6531 | int called_emsg_before = called_emsg; |
| 6532 | int ret = FAIL; |
| 6533 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6534 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6535 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6536 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6537 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6538 | // When using a function that was compiled before: Free old instructions. |
| 6539 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6540 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6541 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6542 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6543 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6544 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6545 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6546 | else |
| 6547 | { |
| 6548 | if (add_def_function(ufunc) == FAIL) |
| 6549 | return FAIL; |
| 6550 | new_def_function = TRUE; |
| 6551 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6552 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6553 | ufunc->uf_def_status = UF_COMPILING; |
| 6554 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6555 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6556 | cctx.ctx_ufunc = ufunc; |
| 6557 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6558 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6559 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6560 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6561 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6562 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6563 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6564 | instr = &cctx.ctx_instr; |
| 6565 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6566 | // Set the context to the function, it may be compiled when called from |
| 6567 | // another script. Set the script version to the most modern one. |
| 6568 | // The line number will be set in next_line_from_context(). |
| 6569 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6570 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6571 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6572 | // Make sure error messages are OK. |
| 6573 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6574 | if (do_estack_push) |
| 6575 | estack_push_ufunc(ufunc, 1); |
| 6576 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6577 | if (ufunc->uf_def_args.ga_len > 0) |
| 6578 | { |
| 6579 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6580 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6581 | int i; |
| 6582 | char_u *arg; |
| 6583 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6584 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6585 | |
| 6586 | // Produce instructions for the default values of optional arguments. |
| 6587 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6588 | // where to start when the function is called, depending on the number |
| 6589 | // of arguments. |
| 6590 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6591 | if (ufunc->uf_def_arg_idx == NULL) |
| 6592 | goto erret; |
| 6593 | for (i = 0; i < count; ++i) |
| 6594 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6595 | garray_T *stack = &cctx.ctx_type_stack; |
| 6596 | type_T *val_type; |
| 6597 | int arg_idx = first_def_arg + i; |
| 6598 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6599 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6600 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6601 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6602 | goto erret; |
| 6603 | |
| 6604 | // If no type specified use the type of the default value. |
| 6605 | // Otherwise check that the default value type matches the |
| 6606 | // specified type. |
| 6607 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6608 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6609 | { |
| 6610 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6611 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6612 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 6613 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 6614 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6615 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6616 | |
| 6617 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6618 | goto erret; |
| 6619 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6620 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6621 | |
| 6622 | if (did_set_arg_type) |
| 6623 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | /* |
| 6627 | * Loop over all the lines of the function and generate instructions. |
| 6628 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6629 | for (;;) |
| 6630 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6631 | exarg_T ea; |
| 6632 | cmdmod_T save_cmdmod; |
| 6633 | int starts_with_colon = FALSE; |
| 6634 | char_u *cmd; |
| 6635 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6636 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6637 | // Bail out on the first error to avoid a flood of errors and report |
| 6638 | // the right line number when inside try/catch. |
| 6639 | if (emsg_before != called_emsg) |
| 6640 | goto erret; |
| 6641 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6642 | if (line != NULL && *line == '|') |
| 6643 | // the line continues after a '|' |
| 6644 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6645 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6646 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6647 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6648 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6649 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6650 | goto erret; |
| 6651 | } |
| 6652 | else |
| 6653 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6654 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6655 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6656 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6657 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6658 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6659 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6660 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6661 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6662 | ea.cmdlinep = &line; |
| 6663 | ea.cmd = skipwhite(line); |
| 6664 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6665 | // Some things can be recognized by the first character. |
| 6666 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6667 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6668 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6669 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6670 | if (ea.cmd[1] != '{') |
| 6671 | { |
| 6672 | line = (char_u *)""; |
| 6673 | continue; |
| 6674 | } |
| 6675 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6676 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6677 | case '}': |
| 6678 | { |
| 6679 | // "}" ends a block scope |
| 6680 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6681 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6682 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6683 | if (stype == BLOCK_SCOPE) |
| 6684 | { |
| 6685 | compile_endblock(&cctx); |
| 6686 | line = ea.cmd; |
| 6687 | } |
| 6688 | else |
| 6689 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6690 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6691 | goto erret; |
| 6692 | } |
| 6693 | if (line != NULL) |
| 6694 | line = skipwhite(ea.cmd + 1); |
| 6695 | continue; |
| 6696 | } |
| 6697 | |
| 6698 | case '{': |
| 6699 | // "{" starts a block scope |
| 6700 | // "{'a': 1}->func() is something else |
| 6701 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6702 | { |
| 6703 | line = compile_block(ea.cmd, &cctx); |
| 6704 | continue; |
| 6705 | } |
| 6706 | break; |
| 6707 | |
| 6708 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6709 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6710 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | /* |
| 6714 | * COMMAND MODIFIERS |
| 6715 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6716 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6717 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6718 | { |
| 6719 | if (errormsg != NULL) |
| 6720 | goto erret; |
| 6721 | // empty line or comment |
| 6722 | line = (char_u *)""; |
| 6723 | continue; |
| 6724 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6725 | // TODO: use modifiers in the command |
| 6726 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6727 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6728 | |
| 6729 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6730 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6731 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6732 | { |
| 6733 | if (*ea.cmd == '(') |
| 6734 | // not for "call()" |
| 6735 | ea.cmd = p; |
| 6736 | else |
| 6737 | ea.cmd = skipwhite(ea.cmd); |
| 6738 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6739 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6740 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6741 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6742 | char_u *pskip; |
| 6743 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6744 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6745 | // find what follows. |
| 6746 | // Skip over "var.member", "var[idx]" and the like. |
| 6747 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6748 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6749 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6750 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6751 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6752 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6753 | char_u *var_end; |
| 6754 | int oplen; |
| 6755 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6756 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6757 | if (ea.cmd[0] == '@') |
| 6758 | var_end = ea.cmd + 2; |
| 6759 | else |
| 6760 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6761 | FNE_CHECK_START | FNE_INCL_BR); |
| 6762 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6763 | if (oplen > 0) |
| 6764 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6765 | size_t len = p - ea.cmd; |
| 6766 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6767 | // Recognize an assignment if we recognize the variable |
| 6768 | // name: |
| 6769 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6770 | // "local = expr" where "local" is a local var. |
| 6771 | // "script = expr" where "script" is a script-local var. |
| 6772 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6773 | // "&opt = expr" |
| 6774 | // "$ENV = expr" |
| 6775 | // "@r = expr" |
| 6776 | if (*ea.cmd == '&' |
| 6777 | || *ea.cmd == '$' |
| 6778 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6779 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6780 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6781 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6782 | NULL, &cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 6783 | || lookup_script(ea.cmd, len, FALSE) == OK |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6784 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6785 | { |
| 6786 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6787 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6788 | goto erret; |
| 6789 | continue; |
| 6790 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6791 | } |
| 6792 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6793 | |
| 6794 | if (*ea.cmd == '[') |
| 6795 | { |
| 6796 | // [var, var] = expr |
| 6797 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6798 | if (line == NULL) |
| 6799 | goto erret; |
| 6800 | if (line != ea.cmd) |
| 6801 | continue; |
| 6802 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6803 | } |
| 6804 | |
| 6805 | /* |
| 6806 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6807 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6808 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6809 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 6810 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6811 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6812 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6813 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6814 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6815 | if (!starts_with_colon) |
| 6816 | { |
| 6817 | emsg(_(e_colon_required_before_a_range)); |
| 6818 | goto erret; |
| 6819 | } |
| 6820 | if (ends_excmd2(line, ea.cmd)) |
| 6821 | { |
| 6822 | // A range without a command: jump to the line. |
| 6823 | // TODO: compile to a more efficient command, possibly |
| 6824 | // calling parse_cmd_address(). |
| 6825 | ea.cmdidx = CMD_SIZE; |
| 6826 | line = compile_exec(line, &ea, &cctx); |
| 6827 | goto nextline; |
| 6828 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6829 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6830 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6831 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6832 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6833 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6834 | |
| 6835 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6836 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6837 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6838 | { |
| 6839 | line += STRLEN(line); |
| 6840 | continue; |
| 6841 | } |
| 6842 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6843 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6844 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6845 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6846 | // CMD_let cannot happen, compile_assignment() above is used |
| 6847 | iemsg("Command from find_ex_command() not handled"); |
| 6848 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6849 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6850 | } |
| 6851 | |
| 6852 | p = skipwhite(p); |
| 6853 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6854 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6855 | && ea.cmdidx != CMD_elseif |
| 6856 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6857 | && ea.cmdidx != CMD_endif |
| 6858 | && ea.cmdidx != CMD_endfor |
| 6859 | && ea.cmdidx != CMD_endwhile |
| 6860 | && ea.cmdidx != CMD_catch |
| 6861 | && ea.cmdidx != CMD_finally |
| 6862 | && ea.cmdidx != CMD_endtry) |
| 6863 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6864 | emsg(_(e_unreachable_code_after_return)); |
| 6865 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6866 | } |
| 6867 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6868 | switch (ea.cmdidx) |
| 6869 | { |
| 6870 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6871 | ea.arg = p; |
| 6872 | line = compile_nested_function(&ea, &cctx); |
| 6873 | break; |
| 6874 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6875 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6876 | // TODO: should we allow this, e.g. to declare a global |
| 6877 | // function? |
| 6878 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6879 | goto erret; |
| 6880 | |
| 6881 | case CMD_return: |
| 6882 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6883 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6884 | break; |
| 6885 | |
| 6886 | case CMD_let: |
| 6887 | case CMD_const: |
| 6888 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6889 | if (line == p) |
| 6890 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6891 | break; |
| 6892 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6893 | case CMD_unlet: |
| 6894 | case CMD_unlockvar: |
| 6895 | case CMD_lockvar: |
| 6896 | line = compile_unletlock(p, &ea, &cctx); |
| 6897 | break; |
| 6898 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6899 | case CMD_import: |
| 6900 | line = compile_import(p, &cctx); |
| 6901 | break; |
| 6902 | |
| 6903 | case CMD_if: |
| 6904 | line = compile_if(p, &cctx); |
| 6905 | break; |
| 6906 | case CMD_elseif: |
| 6907 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6908 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6909 | break; |
| 6910 | case CMD_else: |
| 6911 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6912 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6913 | break; |
| 6914 | case CMD_endif: |
| 6915 | line = compile_endif(p, &cctx); |
| 6916 | break; |
| 6917 | |
| 6918 | case CMD_while: |
| 6919 | line = compile_while(p, &cctx); |
| 6920 | break; |
| 6921 | case CMD_endwhile: |
| 6922 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6923 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6924 | break; |
| 6925 | |
| 6926 | case CMD_for: |
| 6927 | line = compile_for(p, &cctx); |
| 6928 | break; |
| 6929 | case CMD_endfor: |
| 6930 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6931 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6932 | break; |
| 6933 | case CMD_continue: |
| 6934 | line = compile_continue(p, &cctx); |
| 6935 | break; |
| 6936 | case CMD_break: |
| 6937 | line = compile_break(p, &cctx); |
| 6938 | break; |
| 6939 | |
| 6940 | case CMD_try: |
| 6941 | line = compile_try(p, &cctx); |
| 6942 | break; |
| 6943 | case CMD_catch: |
| 6944 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6945 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6946 | break; |
| 6947 | case CMD_finally: |
| 6948 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6949 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6950 | break; |
| 6951 | case CMD_endtry: |
| 6952 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6953 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6954 | break; |
| 6955 | case CMD_throw: |
| 6956 | line = compile_throw(p, &cctx); |
| 6957 | break; |
| 6958 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6959 | case CMD_eval: |
| 6960 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6961 | goto erret; |
| 6962 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6963 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6964 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6965 | |
| 6966 | line = skipwhite(p); |
| 6967 | break; |
| 6968 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6969 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6970 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6971 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6972 | case CMD_echomsg: |
| 6973 | case CMD_echoerr: |
| 6974 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6975 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6976 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6977 | case CMD_put: |
| 6978 | ea.cmd = cmd; |
| 6979 | line = compile_put(p, &ea, &cctx); |
| 6980 | break; |
| 6981 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6982 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6983 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6984 | case CMD_append: |
| 6985 | case CMD_change: |
| 6986 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 6987 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6988 | case CMD_xit: |
| 6989 | not_in_vim9(&ea); |
| 6990 | goto erret; |
| 6991 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6992 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6993 | if (cctx.ctx_skip != SKIP_YES) |
| 6994 | { |
| 6995 | semsg(_(e_invalid_command_str), ea.cmd); |
| 6996 | goto erret; |
| 6997 | } |
| 6998 | // We don't check for a next command here. |
| 6999 | line = (char_u *)""; |
| 7000 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7001 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7002 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 7003 | if (cctx.ctx_skip == SKIP_YES) |
| 7004 | { |
| 7005 | // We don't check for a next command here. |
| 7006 | line = (char_u *)""; |
| 7007 | } |
| 7008 | else |
| 7009 | { |
| 7010 | // Not recognized, execute with do_cmdline_cmd(). |
| 7011 | ea.arg = p; |
| 7012 | line = compile_exec(line, &ea, &cctx); |
| 7013 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7014 | break; |
| 7015 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7016 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7017 | if (line == NULL) |
| 7018 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7019 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7020 | |
| 7021 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7022 | { |
| 7023 | iemsg("Type stack underflow"); |
| 7024 | goto erret; |
| 7025 | } |
| 7026 | } |
| 7027 | |
| 7028 | if (cctx.ctx_scope != NULL) |
| 7029 | { |
| 7030 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7031 | emsg(_(e_endif)); |
| 7032 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7033 | emsg(_(e_endwhile)); |
| 7034 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7035 | emsg(_(e_endfor)); |
| 7036 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7037 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7038 | goto erret; |
| 7039 | } |
| 7040 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7041 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7042 | { |
| 7043 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7044 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7045 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7046 | goto erret; |
| 7047 | } |
| 7048 | |
| 7049 | // Return zero if there is no return at the end. |
| 7050 | generate_PUSHNR(&cctx, 0); |
| 7051 | generate_instr(&cctx, ISN_RETURN); |
| 7052 | } |
| 7053 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7054 | { |
| 7055 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7056 | + ufunc->uf_dfunc_idx; |
| 7057 | dfunc->df_deleted = FALSE; |
| 7058 | dfunc->df_instr = instr->ga_data; |
| 7059 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7060 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7061 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7062 | if (cctx.ctx_outer_used) |
| 7063 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7064 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7065 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7066 | |
| 7067 | ret = OK; |
| 7068 | |
| 7069 | erret: |
| 7070 | if (ret == FAIL) |
| 7071 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7072 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7073 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7074 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7075 | |
| 7076 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7077 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7078 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7079 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7080 | // If using the last entry in the table and it was added above, we |
| 7081 | // might as well remove it. |
| 7082 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7083 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7084 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7085 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7086 | ufunc->uf_dfunc_idx = 0; |
| 7087 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7088 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7089 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7090 | while (cctx.ctx_scope != NULL) |
| 7091 | drop_scope(&cctx); |
| 7092 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7093 | // Don't execute this function body. |
| 7094 | ga_clear_strings(&ufunc->uf_lines); |
| 7095 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7096 | if (errormsg != NULL) |
| 7097 | emsg(errormsg); |
| 7098 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7099 | emsg(_(e_compile_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7100 | } |
| 7101 | |
| 7102 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7103 | if (do_estack_push) |
| 7104 | estack_pop(); |
| 7105 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7106 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7107 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7108 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7109 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7110 | } |
| 7111 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7112 | void |
| 7113 | set_function_type(ufunc_T *ufunc) |
| 7114 | { |
| 7115 | int varargs = ufunc->uf_va_name != NULL; |
| 7116 | int argcount = ufunc->uf_args.ga_len; |
| 7117 | |
| 7118 | // Create a type for the function, with the return type and any |
| 7119 | // argument types. |
| 7120 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7121 | // The type is included in "tt_args". |
| 7122 | if (argcount > 0 || varargs) |
| 7123 | { |
| 7124 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7125 | argcount, &ufunc->uf_type_list); |
| 7126 | // Add argument types to the function type. |
| 7127 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7128 | argcount + varargs, |
| 7129 | &ufunc->uf_type_list) == FAIL) |
| 7130 | return; |
| 7131 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7132 | ufunc->uf_func_type->tt_min_argcount = |
| 7133 | argcount - ufunc->uf_def_args.ga_len; |
| 7134 | if (ufunc->uf_arg_types == NULL) |
| 7135 | { |
| 7136 | int i; |
| 7137 | |
| 7138 | // lambda does not have argument types. |
| 7139 | for (i = 0; i < argcount; ++i) |
| 7140 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7141 | } |
| 7142 | else |
| 7143 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7144 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7145 | if (varargs) |
| 7146 | { |
| 7147 | ufunc->uf_func_type->tt_args[argcount] = |
| 7148 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7149 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7150 | } |
| 7151 | } |
| 7152 | else |
| 7153 | // No arguments, can use a predefined type. |
| 7154 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7155 | argcount, &ufunc->uf_type_list); |
| 7156 | } |
| 7157 | |
| 7158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7159 | /* |
| 7160 | * Delete an instruction, free what it contains. |
| 7161 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7162 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7163 | delete_instr(isn_T *isn) |
| 7164 | { |
| 7165 | switch (isn->isn_type) |
| 7166 | { |
| 7167 | case ISN_EXEC: |
| 7168 | case ISN_LOADENV: |
| 7169 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7170 | case ISN_LOADB: |
| 7171 | case ISN_LOADW: |
| 7172 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7173 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7174 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7175 | case ISN_PUSHEXC: |
| 7176 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7177 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7178 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7179 | case ISN_STOREB: |
| 7180 | case ISN_STOREW: |
| 7181 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7182 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7183 | vim_free(isn->isn_arg.string); |
| 7184 | break; |
| 7185 | |
| 7186 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7187 | case ISN_STORES: |
| 7188 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7189 | break; |
| 7190 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7191 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7192 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7193 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7194 | break; |
| 7195 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7196 | case ISN_STOREOPT: |
| 7197 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7198 | break; |
| 7199 | |
| 7200 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7201 | blob_unref(isn->isn_arg.blob); |
| 7202 | break; |
| 7203 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7204 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7205 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7206 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7207 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7208 | break; |
| 7209 | |
| 7210 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7211 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7212 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7213 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7214 | break; |
| 7215 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7216 | case ISN_UCALL: |
| 7217 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7218 | break; |
| 7219 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7220 | case ISN_FUNCREF: |
| 7221 | { |
| 7222 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7223 | + isn->isn_arg.funcref.fr_func; |
| 7224 | func_ptr_unref(dfunc->df_ufunc); |
| 7225 | } |
| 7226 | break; |
| 7227 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7228 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7229 | { |
| 7230 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7231 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7232 | |
| 7233 | if (ufunc != NULL) |
| 7234 | { |
| 7235 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7236 | clear_def_function(ufunc); |
| 7237 | ufunc->uf_dfunc_idx = 0; |
| 7238 | func_ptr_unref(ufunc); |
| 7239 | } |
| 7240 | |
| 7241 | vim_free(lambda); |
| 7242 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7243 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7244 | break; |
| 7245 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7246 | case ISN_2BOOL: |
| 7247 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 7248 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7249 | case ISN_ADDBLOB: |
| 7250 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7251 | case ISN_ANYINDEX: |
| 7252 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7253 | case ISN_BCALL: |
| 7254 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7255 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7256 | case ISN_CHECKNR: |
| 7257 | case ISN_CHECKTYPE: |
| 7258 | case ISN_COMPAREANY: |
| 7259 | case ISN_COMPAREBLOB: |
| 7260 | case ISN_COMPAREBOOL: |
| 7261 | case ISN_COMPAREDICT: |
| 7262 | case ISN_COMPAREFLOAT: |
| 7263 | case ISN_COMPAREFUNC: |
| 7264 | case ISN_COMPARELIST: |
| 7265 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7266 | case ISN_COMPARESPECIAL: |
| 7267 | case ISN_COMPARESTRING: |
| 7268 | case ISN_CONCAT: |
| 7269 | case ISN_DCALL: |
| 7270 | case ISN_DROP: |
| 7271 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7272 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7273 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7274 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7275 | case ISN_EXECCONCAT: |
| 7276 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7277 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7278 | case ISN_GETITEM: |
| 7279 | case ISN_JUMP: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7280 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 7281 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7282 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7283 | case ISN_LOADBDICT: |
| 7284 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7285 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7286 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7287 | case ISN_LOADSCRIPT: |
| 7288 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7289 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7290 | case ISN_LOADWDICT: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7291 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7292 | case ISN_NEGATENR: |
| 7293 | case ISN_NEWDICT: |
| 7294 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7295 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7296 | case ISN_OPFLOAT: |
| 7297 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7298 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7299 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7300 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7301 | case ISN_PUSHF: |
| 7302 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7303 | case ISN_PUSHSPEC: |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7304 | case ISN_PUT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7305 | case ISN_RETURN: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7306 | case ISN_SHUFFLE: |
| 7307 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7308 | case ISN_STORE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7309 | case ISN_STOREDICT: |
| 7310 | case ISN_STORELIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7311 | case ISN_STORENR: |
| 7312 | case ISN_STOREOUTER: |
| 7313 | case ISN_STOREREG: |
| 7314 | case ISN_STORESCRIPT: |
| 7315 | case ISN_STOREV: |
| 7316 | case ISN_STRINDEX: |
| 7317 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7318 | case ISN_THROW: |
| 7319 | case ISN_TRY: |
| 7320 | // nothing allocated |
| 7321 | break; |
| 7322 | } |
| 7323 | } |
| 7324 | |
| 7325 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7326 | * Free all instructions for "dfunc". |
| 7327 | */ |
| 7328 | static void |
| 7329 | delete_def_function_contents(dfunc_T *dfunc) |
| 7330 | { |
| 7331 | int idx; |
| 7332 | |
| 7333 | ga_clear(&dfunc->df_def_args_isn); |
| 7334 | |
| 7335 | if (dfunc->df_instr != NULL) |
| 7336 | { |
| 7337 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7338 | delete_instr(dfunc->df_instr + idx); |
| 7339 | VIM_CLEAR(dfunc->df_instr); |
| 7340 | } |
| 7341 | |
| 7342 | dfunc->df_deleted = TRUE; |
| 7343 | } |
| 7344 | |
| 7345 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7346 | * When a user function is deleted, clear the contents of any associated def |
| 7347 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7348 | */ |
| 7349 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7350 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7351 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7352 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7353 | { |
| 7354 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7355 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7356 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7357 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7358 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7359 | } |
| 7360 | } |
| 7361 | |
| 7362 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7363 | /* |
| 7364 | * Free all functions defined with ":def". |
| 7365 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7366 | void |
| 7367 | free_def_functions(void) |
| 7368 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7369 | int idx; |
| 7370 | |
| 7371 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7372 | { |
| 7373 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7374 | |
| 7375 | delete_def_function_contents(dfunc); |
| 7376 | } |
| 7377 | |
| 7378 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7379 | } |
| 7380 | #endif |
| 7381 | |
| 7382 | |
| 7383 | #endif // FEAT_EVAL |