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 | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 732 | if (check_type(expected, actual, FALSE, 0) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 733 | return OK; |
| 734 | if (actual->tt_type != VAR_ANY |
| 735 | && actual->tt_type != VAR_UNKNOWN |
| 736 | && !(actual->tt_type == VAR_FUNC |
| 737 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 738 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 739 | if (!silent) |
| 740 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 741 | return FAIL; |
| 742 | } |
| 743 | generate_TYPECHECK(cctx, expected, offset); |
| 744 | return OK; |
| 745 | } |
| 746 | |
| 747 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 748 | * Generate an ISN_PUSHNR instruction. |
| 749 | */ |
| 750 | static int |
| 751 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 752 | { |
| 753 | isn_T *isn; |
| 754 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 755 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 756 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 757 | return FAIL; |
| 758 | isn->isn_arg.number = number; |
| 759 | |
| 760 | return OK; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Generate an ISN_PUSHBOOL instruction. |
| 765 | */ |
| 766 | static int |
| 767 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 768 | { |
| 769 | isn_T *isn; |
| 770 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 771 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 772 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 773 | return FAIL; |
| 774 | isn->isn_arg.number = number; |
| 775 | |
| 776 | return OK; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Generate an ISN_PUSHSPEC instruction. |
| 781 | */ |
| 782 | static int |
| 783 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 784 | { |
| 785 | isn_T *isn; |
| 786 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 787 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 788 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 789 | return FAIL; |
| 790 | isn->isn_arg.number = number; |
| 791 | |
| 792 | return OK; |
| 793 | } |
| 794 | |
| 795 | #ifdef FEAT_FLOAT |
| 796 | /* |
| 797 | * Generate an ISN_PUSHF instruction. |
| 798 | */ |
| 799 | static int |
| 800 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 801 | { |
| 802 | isn_T *isn; |
| 803 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 804 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 805 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 806 | return FAIL; |
| 807 | isn->isn_arg.fnumber = fnumber; |
| 808 | |
| 809 | return OK; |
| 810 | } |
| 811 | #endif |
| 812 | |
| 813 | /* |
| 814 | * Generate an ISN_PUSHS instruction. |
| 815 | * Consumes "str". |
| 816 | */ |
| 817 | static int |
| 818 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 819 | { |
| 820 | isn_T *isn; |
| 821 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 822 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 823 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 824 | return FAIL; |
| 825 | isn->isn_arg.string = str; |
| 826 | |
| 827 | return OK; |
| 828 | } |
| 829 | |
| 830 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 831 | * Generate an ISN_PUSHCHANNEL instruction. |
| 832 | * Consumes "channel". |
| 833 | */ |
| 834 | static int |
| 835 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 836 | { |
| 837 | isn_T *isn; |
| 838 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 839 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 840 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 841 | return FAIL; |
| 842 | isn->isn_arg.channel = channel; |
| 843 | |
| 844 | return OK; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Generate an ISN_PUSHJOB instruction. |
| 849 | * Consumes "job". |
| 850 | */ |
| 851 | static int |
| 852 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 853 | { |
| 854 | isn_T *isn; |
| 855 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 856 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 857 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 858 | return FAIL; |
| 859 | isn->isn_arg.job = job; |
| 860 | |
| 861 | return OK; |
| 862 | } |
| 863 | |
| 864 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 865 | * Generate an ISN_PUSHBLOB instruction. |
| 866 | * Consumes "blob". |
| 867 | */ |
| 868 | static int |
| 869 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 870 | { |
| 871 | isn_T *isn; |
| 872 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 873 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 875 | return FAIL; |
| 876 | isn->isn_arg.blob = blob; |
| 877 | |
| 878 | return OK; |
| 879 | } |
| 880 | |
| 881 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 882 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 883 | * Consumes "name". |
| 884 | */ |
| 885 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 886 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 887 | { |
| 888 | isn_T *isn; |
| 889 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 890 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 891 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 892 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 893 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 894 | |
| 895 | return OK; |
| 896 | } |
| 897 | |
| 898 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 899 | * Generate an ISN_GETITEM instruction with "index". |
| 900 | */ |
| 901 | static int |
| 902 | generate_GETITEM(cctx_T *cctx, int index) |
| 903 | { |
| 904 | isn_T *isn; |
| 905 | garray_T *stack = &cctx->ctx_type_stack; |
| 906 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 907 | type_T *item_type = &t_any; |
| 908 | |
| 909 | RETURN_OK_IF_SKIP(cctx); |
| 910 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 911 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 912 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 913 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 914 | emsg(_(e_listreq)); |
| 915 | return FAIL; |
| 916 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 917 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 918 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 919 | return FAIL; |
| 920 | isn->isn_arg.number = index; |
| 921 | |
| 922 | // add the item type to the type stack |
| 923 | if (ga_grow(stack, 1) == FAIL) |
| 924 | return FAIL; |
| 925 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 926 | ++stack->ga_len; |
| 927 | return OK; |
| 928 | } |
| 929 | |
| 930 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 931 | * Generate an ISN_SLICE instruction with "count". |
| 932 | */ |
| 933 | static int |
| 934 | generate_SLICE(cctx_T *cctx, int count) |
| 935 | { |
| 936 | isn_T *isn; |
| 937 | |
| 938 | RETURN_OK_IF_SKIP(cctx); |
| 939 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 940 | return FAIL; |
| 941 | isn->isn_arg.number = count; |
| 942 | return OK; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 947 | */ |
| 948 | static int |
| 949 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 950 | { |
| 951 | isn_T *isn; |
| 952 | |
| 953 | RETURN_OK_IF_SKIP(cctx); |
| 954 | |
| 955 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 956 | return FAIL; |
| 957 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 958 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 959 | |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 964 | * Generate an ISN_STORE instruction. |
| 965 | */ |
| 966 | static int |
| 967 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 968 | { |
| 969 | isn_T *isn; |
| 970 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 971 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 972 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 973 | return FAIL; |
| 974 | if (name != NULL) |
| 975 | isn->isn_arg.string = vim_strsave(name); |
| 976 | else |
| 977 | isn->isn_arg.number = idx; |
| 978 | |
| 979 | return OK; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 984 | */ |
| 985 | static int |
| 986 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 987 | { |
| 988 | isn_T *isn; |
| 989 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 990 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 991 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 992 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 993 | isn->isn_arg.storenr.stnr_idx = idx; |
| 994 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 995 | |
| 996 | return OK; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * Generate an ISN_STOREOPT instruction |
| 1001 | */ |
| 1002 | static int |
| 1003 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1004 | { |
| 1005 | isn_T *isn; |
| 1006 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1007 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1008 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1009 | return FAIL; |
| 1010 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1011 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1012 | |
| 1013 | return OK; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Generate an ISN_LOAD or similar instruction. |
| 1018 | */ |
| 1019 | static int |
| 1020 | generate_LOAD( |
| 1021 | cctx_T *cctx, |
| 1022 | isntype_T isn_type, |
| 1023 | int idx, |
| 1024 | char_u *name, |
| 1025 | type_T *type) |
| 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 | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1030 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1031 | return FAIL; |
| 1032 | if (name != NULL) |
| 1033 | isn->isn_arg.string = vim_strsave(name); |
| 1034 | else |
| 1035 | isn->isn_arg.number = idx; |
| 1036 | |
| 1037 | return OK; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1041 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1042 | */ |
| 1043 | static int |
| 1044 | generate_LOADV( |
| 1045 | cctx_T *cctx, |
| 1046 | char_u *name, |
| 1047 | int error) |
| 1048 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1049 | int di_flags; |
| 1050 | int vidx = find_vim_var(name, &di_flags); |
| 1051 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1052 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1053 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1054 | if (vidx < 0) |
| 1055 | { |
| 1056 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1057 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1058 | return FAIL; |
| 1059 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1060 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1061 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1062 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1066 | * Generate an ISN_UNLET instruction. |
| 1067 | */ |
| 1068 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1069 | 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] | 1070 | { |
| 1071 | isn_T *isn; |
| 1072 | |
| 1073 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1074 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1075 | return FAIL; |
| 1076 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1077 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1078 | |
| 1079 | return OK; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1083 | * Generate an ISN_LOADS instruction. |
| 1084 | */ |
| 1085 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1086 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1087 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1088 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1089 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1090 | int sid, |
| 1091 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1092 | { |
| 1093 | isn_T *isn; |
| 1094 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1095 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1096 | if (isn_type == ISN_LOADS) |
| 1097 | isn = generate_instr_type(cctx, isn_type, type); |
| 1098 | else |
| 1099 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1100 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1101 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1102 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1103 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1104 | |
| 1105 | return OK; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1110 | */ |
| 1111 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1112 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1113 | cctx_T *cctx, |
| 1114 | isntype_T isn_type, |
| 1115 | int sid, |
| 1116 | int idx, |
| 1117 | type_T *type) |
| 1118 | { |
| 1119 | isn_T *isn; |
| 1120 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1121 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1122 | if (isn_type == ISN_LOADSCRIPT) |
| 1123 | isn = generate_instr_type(cctx, isn_type, type); |
| 1124 | else |
| 1125 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1126 | if (isn == NULL) |
| 1127 | return FAIL; |
| 1128 | isn->isn_arg.script.script_sid = sid; |
| 1129 | isn->isn_arg.script.script_idx = idx; |
| 1130 | return OK; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Generate an ISN_NEWLIST instruction. |
| 1135 | */ |
| 1136 | static int |
| 1137 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1138 | { |
| 1139 | isn_T *isn; |
| 1140 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1141 | type_T *type; |
| 1142 | type_T *member; |
| 1143 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1144 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1145 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1146 | return FAIL; |
| 1147 | isn->isn_arg.number = count; |
| 1148 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1149 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1150 | if (count == 0) |
| 1151 | member = &t_void; |
| 1152 | else |
| 1153 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1154 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1155 | cctx->ctx_type_list); |
| 1156 | type = get_list_type(member, cctx->ctx_type_list); |
| 1157 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1158 | // drop the value types |
| 1159 | stack->ga_len -= count; |
| 1160 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1161 | // add the list type to the type stack |
| 1162 | if (ga_grow(stack, 1) == FAIL) |
| 1163 | return FAIL; |
| 1164 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1165 | ++stack->ga_len; |
| 1166 | |
| 1167 | return OK; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * Generate an ISN_NEWDICT instruction. |
| 1172 | */ |
| 1173 | static int |
| 1174 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1175 | { |
| 1176 | isn_T *isn; |
| 1177 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1178 | type_T *type; |
| 1179 | type_T *member; |
| 1180 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1181 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1182 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1183 | return FAIL; |
| 1184 | isn->isn_arg.number = count; |
| 1185 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1186 | if (count == 0) |
| 1187 | member = &t_void; |
| 1188 | else |
| 1189 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1190 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1191 | cctx->ctx_type_list); |
| 1192 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1193 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1194 | // drop the key and value types |
| 1195 | stack->ga_len -= 2 * count; |
| 1196 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1197 | // add the dict type to the type stack |
| 1198 | if (ga_grow(stack, 1) == FAIL) |
| 1199 | return FAIL; |
| 1200 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1201 | ++stack->ga_len; |
| 1202 | |
| 1203 | return OK; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Generate an ISN_FUNCREF instruction. |
| 1208 | */ |
| 1209 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1210 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1211 | { |
| 1212 | isn_T *isn; |
| 1213 | garray_T *stack = &cctx->ctx_type_stack; |
| 1214 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1215 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1216 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1217 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1218 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1219 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1220 | |
| 1221 | if (ga_grow(stack, 1) == FAIL) |
| 1222 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1223 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1224 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1225 | ++stack->ga_len; |
| 1226 | |
| 1227 | return OK; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1231 | * Generate an ISN_NEWFUNC instruction. |
| 1232 | */ |
| 1233 | static int |
| 1234 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1235 | { |
| 1236 | isn_T *isn; |
| 1237 | char_u *name; |
| 1238 | |
| 1239 | RETURN_OK_IF_SKIP(cctx); |
| 1240 | name = vim_strsave(lambda_name); |
| 1241 | if (name == NULL) |
| 1242 | return FAIL; |
| 1243 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1244 | return FAIL; |
| 1245 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1246 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1247 | |
| 1248 | return OK; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1252 | * Generate an ISN_JUMP instruction. |
| 1253 | */ |
| 1254 | static int |
| 1255 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1256 | { |
| 1257 | isn_T *isn; |
| 1258 | garray_T *stack = &cctx->ctx_type_stack; |
| 1259 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1260 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1261 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1262 | return FAIL; |
| 1263 | isn->isn_arg.jump.jump_when = when; |
| 1264 | isn->isn_arg.jump.jump_where = where; |
| 1265 | |
| 1266 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1267 | --stack->ga_len; |
| 1268 | |
| 1269 | return OK; |
| 1270 | } |
| 1271 | |
| 1272 | static int |
| 1273 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1274 | { |
| 1275 | isn_T *isn; |
| 1276 | garray_T *stack = &cctx->ctx_type_stack; |
| 1277 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1278 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1279 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1280 | return FAIL; |
| 1281 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1282 | |
| 1283 | if (ga_grow(stack, 1) == FAIL) |
| 1284 | return FAIL; |
| 1285 | // type doesn't matter, will be stored next |
| 1286 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1287 | ++stack->ga_len; |
| 1288 | |
| 1289 | return OK; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1294 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1295 | * Return FAIL if the number of arguments is wrong. |
| 1296 | */ |
| 1297 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1298 | 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] | 1299 | { |
| 1300 | isn_T *isn; |
| 1301 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1302 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1303 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1304 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1305 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1306 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1307 | argoff = check_internal_func(func_idx, argcount); |
| 1308 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1309 | return FAIL; |
| 1310 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1311 | if (method_call && argoff > 1) |
| 1312 | { |
| 1313 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1314 | return FAIL; |
| 1315 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1316 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1317 | } |
| 1318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1319 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1320 | return FAIL; |
| 1321 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1322 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1323 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1324 | for (i = 0; i < argcount; ++i) |
| 1325 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1326 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1327 | stack->ga_len -= argcount; // drop the arguments |
| 1328 | if (ga_grow(stack, 1) == FAIL) |
| 1329 | return FAIL; |
| 1330 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1331 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1332 | ++stack->ga_len; // add return value |
| 1333 | |
| 1334 | return OK; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1339 | * Return FAIL if the number of arguments is wrong. |
| 1340 | */ |
| 1341 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1342 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1343 | { |
| 1344 | isn_T *isn; |
| 1345 | garray_T *stack = &cctx->ctx_type_stack; |
| 1346 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1347 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1348 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1349 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1350 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1351 | { |
| 1352 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1353 | return FAIL; |
| 1354 | } |
| 1355 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1356 | { |
| 1357 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1358 | return FAIL; |
| 1359 | } |
| 1360 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1361 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1362 | { |
| 1363 | int i; |
| 1364 | |
| 1365 | for (i = 0; i < argcount; ++i) |
| 1366 | { |
| 1367 | type_T *expected; |
| 1368 | type_T *actual; |
| 1369 | |
| 1370 | if (i < regular_args) |
| 1371 | { |
| 1372 | if (ufunc->uf_arg_types == NULL) |
| 1373 | continue; |
| 1374 | expected = ufunc->uf_arg_types[i]; |
| 1375 | } |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1376 | else if (ufunc->uf_va_type == NULL) |
| 1377 | // possibly a lambda |
| 1378 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1379 | else |
| 1380 | expected = ufunc->uf_va_type->tt_member; |
| 1381 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1382 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1383 | { |
| 1384 | arg_type_mismatch(expected, actual, i + 1); |
| 1385 | return FAIL; |
| 1386 | } |
| 1387 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1388 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1389 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1390 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1391 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1392 | } |
| 1393 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1394 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1395 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1396 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1397 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1398 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1399 | { |
| 1400 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1401 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1402 | } |
| 1403 | else |
| 1404 | { |
| 1405 | // A user function may be deleted and redefined later, can't use the |
| 1406 | // ufunc pointer, need to look it up again at runtime. |
| 1407 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1408 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1409 | } |
| 1410 | |
| 1411 | stack->ga_len -= argcount; // drop the arguments |
| 1412 | if (ga_grow(stack, 1) == FAIL) |
| 1413 | return FAIL; |
| 1414 | // add return value |
| 1415 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1416 | ++stack->ga_len; |
| 1417 | |
| 1418 | return OK; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1423 | */ |
| 1424 | static int |
| 1425 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1426 | { |
| 1427 | isn_T *isn; |
| 1428 | garray_T *stack = &cctx->ctx_type_stack; |
| 1429 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1430 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1431 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1432 | return FAIL; |
| 1433 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1434 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1435 | |
| 1436 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1437 | if (ga_grow(stack, 1) == FAIL) |
| 1438 | return FAIL; |
| 1439 | // add return value |
| 1440 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1441 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1442 | |
| 1443 | return OK; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
| 1447 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1448 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1449 | */ |
| 1450 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1451 | generate_PCALL( |
| 1452 | cctx_T *cctx, |
| 1453 | int argcount, |
| 1454 | char_u *name, |
| 1455 | type_T *type, |
| 1456 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1457 | { |
| 1458 | isn_T *isn; |
| 1459 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1460 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1461 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1462 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1463 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1464 | if (type->tt_type == VAR_ANY) |
| 1465 | ret_type = &t_any; |
| 1466 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1467 | { |
| 1468 | if (type->tt_argcount != -1) |
| 1469 | { |
| 1470 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1471 | |
| 1472 | if (argcount < type->tt_min_argcount - varargs) |
| 1473 | { |
| 1474 | semsg(_(e_toofewarg), "[reference]"); |
| 1475 | return FAIL; |
| 1476 | } |
| 1477 | if (!varargs && argcount > type->tt_argcount) |
| 1478 | { |
| 1479 | semsg(_(e_toomanyarg), "[reference]"); |
| 1480 | return FAIL; |
| 1481 | } |
| 1482 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1483 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1484 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1485 | else |
| 1486 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1487 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1488 | return FAIL; |
| 1489 | } |
| 1490 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1492 | return FAIL; |
| 1493 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1494 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1495 | |
| 1496 | stack->ga_len -= argcount; // drop the arguments |
| 1497 | |
| 1498 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1499 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1500 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1501 | // If partial is above the arguments it must be cleared and replaced with |
| 1502 | // the return value. |
| 1503 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1504 | return FAIL; |
| 1505 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1506 | return OK; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1510 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | */ |
| 1512 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1513 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1514 | { |
| 1515 | isn_T *isn; |
| 1516 | garray_T *stack = &cctx->ctx_type_stack; |
| 1517 | type_T *type; |
| 1518 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1519 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1520 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1521 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1522 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1523 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1524 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1525 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1526 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1527 | { |
| 1528 | emsg(_(e_dictreq)); |
| 1529 | return FAIL; |
| 1530 | } |
| 1531 | // change dict type to dict member type |
| 1532 | if (type->tt_type == VAR_DICT) |
| 1533 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1534 | |
| 1535 | return OK; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * Generate an ISN_ECHO instruction. |
| 1540 | */ |
| 1541 | static int |
| 1542 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1543 | { |
| 1544 | isn_T *isn; |
| 1545 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1546 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1547 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1548 | return FAIL; |
| 1549 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1550 | isn->isn_arg.echo.echo_count = count; |
| 1551 | |
| 1552 | return OK; |
| 1553 | } |
| 1554 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1555 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1556 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1557 | */ |
| 1558 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1559 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1560 | { |
| 1561 | isn_T *isn; |
| 1562 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1563 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1564 | return FAIL; |
| 1565 | isn->isn_arg.number = count; |
| 1566 | |
| 1567 | return OK; |
| 1568 | } |
| 1569 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1570 | static int |
| 1571 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1572 | { |
| 1573 | isn_T *isn; |
| 1574 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1575 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1576 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1577 | return FAIL; |
| 1578 | isn->isn_arg.string = vim_strsave(line); |
| 1579 | return OK; |
| 1580 | } |
| 1581 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1582 | static int |
| 1583 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1584 | { |
| 1585 | isn_T *isn; |
| 1586 | |
| 1587 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1588 | return FAIL; |
| 1589 | isn->isn_arg.number = count; |
| 1590 | return OK; |
| 1591 | } |
| 1592 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1593 | /* |
| 1594 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1595 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1596 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1597 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1598 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1599 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1600 | lvar_T *lvar; |
| 1601 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1602 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1603 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1604 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1605 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1609 | return NULL; |
| 1610 | 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] | 1611 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1612 | // Every local variable uses the next entry on the stack. We could re-use |
| 1613 | // the last ones when leaving a scope, but then variables used in a closure |
| 1614 | // might get overwritten. To keep things simple do not re-use stack |
| 1615 | // entries. This is less efficient, but memory is cheap these days. |
| 1616 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1617 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1618 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1619 | lvar->lv_const = isConst; |
| 1620 | lvar->lv_type = type; |
| 1621 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1622 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1626 | * Remove local variables above "new_top". |
| 1627 | */ |
| 1628 | static void |
| 1629 | unwind_locals(cctx_T *cctx, int new_top) |
| 1630 | { |
| 1631 | if (cctx->ctx_locals.ga_len > new_top) |
| 1632 | { |
| 1633 | int idx; |
| 1634 | lvar_T *lvar; |
| 1635 | |
| 1636 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1637 | { |
| 1638 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1639 | vim_free(lvar->lv_name); |
| 1640 | } |
| 1641 | } |
| 1642 | cctx->ctx_locals.ga_len = new_top; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Free all local variables. |
| 1647 | */ |
| 1648 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1649 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1650 | { |
| 1651 | unwind_locals(cctx, 0); |
| 1652 | ga_clear(&cctx->ctx_locals); |
| 1653 | } |
| 1654 | |
| 1655 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1656 | * Find "name" in script-local items of script "sid". |
| 1657 | * Returns the index in "sn_var_vals" if found. |
| 1658 | * If found but not in "sn_var_vals" returns -1. |
| 1659 | * If not found returns -2. |
| 1660 | */ |
| 1661 | int |
| 1662 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1663 | { |
| 1664 | hashtab_T *ht; |
| 1665 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1666 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1667 | int idx; |
| 1668 | |
| 1669 | // First look the name up in the hashtable. |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1670 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1671 | return -1; |
| 1672 | ht = &SCRIPT_VARS(sid); |
| 1673 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1674 | if (di == NULL) |
| 1675 | return -2; |
| 1676 | |
| 1677 | // Now find the svar_T index in sn_var_vals. |
| 1678 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1679 | { |
| 1680 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1681 | |
| 1682 | if (sv->sv_tv == &di->di_tv) |
| 1683 | { |
| 1684 | if (check_writable && sv->sv_const) |
| 1685 | semsg(_(e_readonlyvar), name); |
| 1686 | return idx; |
| 1687 | } |
| 1688 | } |
| 1689 | return -1; |
| 1690 | } |
| 1691 | |
| 1692 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1693 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1694 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1695 | */ |
| 1696 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1697 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1698 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1699 | int idx; |
| 1700 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1701 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1702 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1703 | if (cctx != NULL) |
| 1704 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1705 | { |
| 1706 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1707 | + idx; |
| 1708 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1709 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1710 | : STRLEN(import->imp_name) == len |
| 1711 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1712 | return import; |
| 1713 | } |
| 1714 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1715 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1716 | } |
| 1717 | |
| 1718 | imported_T * |
| 1719 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1720 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1721 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1722 | int idx; |
| 1723 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1724 | if (!SCRIPT_ID_VALID(sid)) |
| 1725 | return NULL; |
| 1726 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1727 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1728 | { |
| 1729 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1730 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1731 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1732 | : STRLEN(import->imp_name) == len |
| 1733 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1734 | return import; |
| 1735 | } |
| 1736 | return NULL; |
| 1737 | } |
| 1738 | |
| 1739 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1740 | * Free all imported variables. |
| 1741 | */ |
| 1742 | static void |
| 1743 | free_imported(cctx_T *cctx) |
| 1744 | { |
| 1745 | int idx; |
| 1746 | |
| 1747 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1748 | { |
| 1749 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1750 | |
| 1751 | vim_free(import->imp_name); |
| 1752 | } |
| 1753 | ga_clear(&cctx->ctx_imports); |
| 1754 | } |
| 1755 | |
| 1756 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1757 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1758 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1759 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1760 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1761 | { |
| 1762 | return p[0] == '#' && p[1] != '{'; |
| 1763 | } |
| 1764 | |
| 1765 | /* |
| 1766 | * Return a pointer to the next line that isn't empty or only contains a |
| 1767 | * comment. Skips over white space. |
| 1768 | * Returns NULL if there is none. |
| 1769 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1770 | char_u * |
| 1771 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1772 | { |
| 1773 | int lnum = cctx->ctx_lnum; |
| 1774 | |
| 1775 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1776 | { |
| 1777 | 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] | 1778 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1779 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 1780 | // ignore NULLs inserted for continuation lines |
| 1781 | if (line != NULL) |
| 1782 | { |
| 1783 | p = skipwhite(line); |
| 1784 | if (*p != NUL && !vim9_comment_start(p)) |
| 1785 | return p; |
| 1786 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1787 | } |
| 1788 | return NULL; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1792 | * Called when checking for a following operator at "arg". When the rest of |
| 1793 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1794 | * line return a pointer to it and set "nextp". |
| 1795 | * Otherwise skip over white space. |
| 1796 | */ |
| 1797 | static char_u * |
| 1798 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1799 | { |
| 1800 | char_u *p = skipwhite(arg); |
| 1801 | |
| 1802 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1803 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1804 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1805 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1806 | if (*nextp != NULL) |
| 1807 | return *nextp; |
| 1808 | } |
| 1809 | return p; |
| 1810 | } |
| 1811 | |
| 1812 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1813 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1814 | * 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] | 1815 | * Returns NULL when at the end. |
| 1816 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1817 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1818 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1819 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1820 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1821 | |
| 1822 | do |
| 1823 | { |
| 1824 | ++cctx->ctx_lnum; |
| 1825 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1826 | { |
| 1827 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1828 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1829 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1830 | 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] | 1831 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1832 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1833 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1834 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1835 | return line; |
| 1836 | } |
| 1837 | |
| 1838 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1839 | * 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] | 1840 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1841 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1842 | */ |
| 1843 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1844 | 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] | 1845 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1846 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1847 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1848 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1849 | |
| 1850 | if (next == NULL) |
| 1851 | return FAIL; |
| 1852 | *arg = skipwhite(next); |
| 1853 | } |
| 1854 | return OK; |
| 1855 | } |
| 1856 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1857 | /* |
| 1858 | * Idem, and give an error when failed. |
| 1859 | */ |
| 1860 | static int |
| 1861 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1862 | { |
| 1863 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1864 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1865 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1866 | return FAIL; |
| 1867 | } |
| 1868 | return OK; |
| 1869 | } |
| 1870 | |
| 1871 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1872 | // Structure passed between the compile_expr* functions to keep track of |
| 1873 | // constants that have been parsed but for which no code was produced yet. If |
| 1874 | // possible expressions on these constants are applied at compile time. If |
| 1875 | // that is not possible, the code to push the constants needs to be generated |
| 1876 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1877 | // Using 50 should be more than enough of 5 levels of (). |
| 1878 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1879 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1880 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1881 | int pp_used; // active entries in pp_tv[] |
| 1882 | } ppconst_T; |
| 1883 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1884 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1885 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1886 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1887 | /* |
| 1888 | * Generate a PUSH instruction for "tv". |
| 1889 | * "tv" will be consumed or cleared. |
| 1890 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1891 | */ |
| 1892 | static int |
| 1893 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1894 | { |
| 1895 | if (tv != NULL) |
| 1896 | { |
| 1897 | switch (tv->v_type) |
| 1898 | { |
| 1899 | case VAR_UNKNOWN: |
| 1900 | break; |
| 1901 | case VAR_BOOL: |
| 1902 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1903 | break; |
| 1904 | case VAR_SPECIAL: |
| 1905 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1906 | break; |
| 1907 | case VAR_NUMBER: |
| 1908 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1909 | break; |
| 1910 | #ifdef FEAT_FLOAT |
| 1911 | case VAR_FLOAT: |
| 1912 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1913 | break; |
| 1914 | #endif |
| 1915 | case VAR_BLOB: |
| 1916 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1917 | tv->vval.v_blob = NULL; |
| 1918 | break; |
| 1919 | case VAR_STRING: |
| 1920 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1921 | tv->vval.v_string = NULL; |
| 1922 | break; |
| 1923 | default: |
| 1924 | iemsg("constant type not supported"); |
| 1925 | clear_tv(tv); |
| 1926 | return FAIL; |
| 1927 | } |
| 1928 | tv->v_type = VAR_UNKNOWN; |
| 1929 | } |
| 1930 | return OK; |
| 1931 | } |
| 1932 | |
| 1933 | /* |
| 1934 | * Generate code for any ppconst entries. |
| 1935 | */ |
| 1936 | static int |
| 1937 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1938 | { |
| 1939 | int i; |
| 1940 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1941 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1942 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1943 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1944 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1945 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1946 | ret = FAIL; |
| 1947 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1948 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1949 | return ret; |
| 1950 | } |
| 1951 | |
| 1952 | /* |
| 1953 | * Clear ppconst constants. Used when failing. |
| 1954 | */ |
| 1955 | static void |
| 1956 | clear_ppconst(ppconst_T *ppconst) |
| 1957 | { |
| 1958 | int i; |
| 1959 | |
| 1960 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1961 | clear_tv(&ppconst->pp_tv[i]); |
| 1962 | ppconst->pp_used = 0; |
| 1963 | } |
| 1964 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1965 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1966 | * Generate an instruction to load script-local variable "name", without the |
| 1967 | * leading "s:". |
| 1968 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1969 | */ |
| 1970 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1971 | compile_load_scriptvar( |
| 1972 | cctx_T *cctx, |
| 1973 | char_u *name, // variable NUL terminated |
| 1974 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 1975 | char_u **end, // end of variable |
| 1976 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1977 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1978 | scriptitem_T *si; |
| 1979 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1980 | imported_T *import; |
| 1981 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1982 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 1983 | return FAIL; |
| 1984 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 1985 | idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1986 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1987 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1988 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1989 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 1990 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1991 | } |
| 1992 | if (idx >= 0) |
| 1993 | { |
| 1994 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1995 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1996 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1997 | current_sctx.sc_sid, idx, sv->sv_type); |
| 1998 | return OK; |
| 1999 | } |
| 2000 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2001 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2002 | if (import != NULL) |
| 2003 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2004 | if (import->imp_all) |
| 2005 | { |
| 2006 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2007 | char_u *exp_name; |
| 2008 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2009 | ufunc_T *ufunc; |
| 2010 | type_T *type; |
| 2011 | |
| 2012 | // Used "import * as Name", need to lookup the member. |
| 2013 | if (*p != '.') |
| 2014 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2015 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2016 | return FAIL; |
| 2017 | } |
| 2018 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2019 | if (VIM_ISWHITE(*p)) |
| 2020 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2021 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2022 | return FAIL; |
| 2023 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2024 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2025 | // isolate one name |
| 2026 | exp_name = p; |
| 2027 | while (eval_isnamec(*p)) |
| 2028 | ++p; |
| 2029 | cc = *p; |
| 2030 | *p = NUL; |
| 2031 | |
| 2032 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2033 | *p = cc; |
| 2034 | p = skipwhite(p); |
| 2035 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2036 | // TODO: what if it is a function? |
| 2037 | if (idx < 0) |
| 2038 | return FAIL; |
| 2039 | *end = p; |
| 2040 | |
| 2041 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2042 | import->imp_sid, |
| 2043 | idx, |
| 2044 | type); |
| 2045 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2046 | else if (import->imp_funcname != NULL) |
| 2047 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2048 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2049 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2050 | import->imp_sid, |
| 2051 | import->imp_var_vals_idx, |
| 2052 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2053 | return OK; |
| 2054 | } |
| 2055 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2056 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2057 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2058 | return FAIL; |
| 2059 | } |
| 2060 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2061 | static int |
| 2062 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2063 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2064 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2065 | |
| 2066 | if (ufunc == NULL) |
| 2067 | return FAIL; |
| 2068 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2069 | // Need to compile any default values to get the argument types. |
| 2070 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2071 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2072 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2073 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2076 | /* |
| 2077 | * Compile a variable name into a load instruction. |
| 2078 | * "end" points to just after the name. |
| 2079 | * When "error" is FALSE do not give an error when not found. |
| 2080 | */ |
| 2081 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2082 | 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] | 2083 | { |
| 2084 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2085 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2086 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2087 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2088 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | |
| 2090 | if (*(*arg + 1) == ':') |
| 2091 | { |
| 2092 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2093 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2094 | { |
| 2095 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2096 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2097 | switch (**arg) |
| 2098 | { |
| 2099 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2100 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2101 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2102 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2103 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2104 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2105 | goto theend; |
| 2106 | } |
| 2107 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2108 | goto theend; |
| 2109 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2110 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2111 | else |
| 2112 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2113 | isntype_T isn_type = ISN_DROP; |
| 2114 | |
| 2115 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2116 | if (name == NULL) |
| 2117 | return FAIL; |
| 2118 | |
| 2119 | switch (**arg) |
| 2120 | { |
| 2121 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2122 | break; |
| 2123 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2124 | NULL, NULL, error); |
| 2125 | break; |
| 2126 | case 'g': isn_type = ISN_LOADG; break; |
| 2127 | case 'w': isn_type = ISN_LOADW; break; |
| 2128 | case 't': isn_type = ISN_LOADT; break; |
| 2129 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2130 | default: semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2131 | goto theend; |
| 2132 | } |
| 2133 | if (isn_type != ISN_DROP) |
| 2134 | { |
| 2135 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2136 | // variables can be defined later, thus we don't check if it |
| 2137 | // exists, give error at runtime. |
| 2138 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2139 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | else |
| 2143 | { |
| 2144 | size_t len = end - *arg; |
| 2145 | int idx; |
| 2146 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2147 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2148 | |
| 2149 | name = vim_strnsave(*arg, end - *arg); |
| 2150 | if (name == NULL) |
| 2151 | return FAIL; |
| 2152 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2153 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2154 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2155 | if (!gen_load_outer) |
| 2156 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2157 | } |
| 2158 | else |
| 2159 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2160 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2161 | |
| 2162 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2163 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2164 | type = lvar->lv_type; |
| 2165 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2166 | if (lvar->lv_from_outer) |
| 2167 | gen_load_outer = TRUE; |
| 2168 | else |
| 2169 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2170 | } |
| 2171 | else |
| 2172 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2173 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2174 | // already exists in a Vim9 script or when it's imported. |
| 2175 | if (lookup_script(*arg, len, TRUE) == OK |
| 2176 | || find_imported(name, 0, cctx) != NULL) |
| 2177 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2178 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2179 | // When the name starts with an uppercase letter or "x:" it |
| 2180 | // can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2181 | // TODO: this is just guessing |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2182 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2183 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | if (gen_load) |
| 2187 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2188 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2189 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2190 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2191 | cctx->ctx_outer_used = TRUE; |
| 2192 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | *arg = end; |
| 2196 | |
| 2197 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2198 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2199 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2200 | vim_free(name); |
| 2201 | return res; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * Compile the argument expressions. |
| 2206 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2207 | */ |
| 2208 | static int |
| 2209 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2210 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2211 | char_u *p = *arg; |
| 2212 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2213 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2214 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2215 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2216 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2217 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2218 | if (*p == ')') |
| 2219 | { |
| 2220 | *arg = p + 1; |
| 2221 | return OK; |
| 2222 | } |
| 2223 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2224 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2225 | return FAIL; |
| 2226 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2227 | |
| 2228 | if (*p != ',' && *skipwhite(p) == ',') |
| 2229 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2230 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2231 | p = skipwhite(p); |
| 2232 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2233 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2234 | { |
| 2235 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2236 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2237 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2238 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2239 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2240 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2241 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2242 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2243 | emsg(_(e_missing_close)); |
| 2244 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Compile a function call: name(arg1, arg2) |
| 2249 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2250 | * "argcount_init" is 1 for "value->method()" |
| 2251 | * Instructions: |
| 2252 | * EVAL arg1 |
| 2253 | * EVAL arg2 |
| 2254 | * BCALL / DCALL / UCALL |
| 2255 | */ |
| 2256 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2257 | compile_call( |
| 2258 | char_u **arg, |
| 2259 | size_t varlen, |
| 2260 | cctx_T *cctx, |
| 2261 | ppconst_T *ppconst, |
| 2262 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2263 | { |
| 2264 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2265 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2266 | int argcount = argcount_init; |
| 2267 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2268 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2269 | char_u *tofree = NULL; |
| 2270 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2271 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2272 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2273 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2274 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2275 | // we can evaluate "has('name')" at compile time |
| 2276 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2277 | { |
| 2278 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2279 | typval_T argvars[2]; |
| 2280 | |
| 2281 | argvars[0].v_type = VAR_UNKNOWN; |
| 2282 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2283 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2284 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2285 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2286 | s = skipwhite(s); |
| 2287 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2288 | { |
| 2289 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2290 | |
| 2291 | *arg = s + 1; |
| 2292 | argvars[1].v_type = VAR_UNKNOWN; |
| 2293 | tv->v_type = VAR_NUMBER; |
| 2294 | tv->vval.v_number = 0; |
| 2295 | f_has(argvars, tv); |
| 2296 | clear_tv(&argvars[0]); |
| 2297 | ++ppconst->pp_used; |
| 2298 | return OK; |
| 2299 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2300 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2304 | return FAIL; |
| 2305 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2306 | if (varlen >= sizeof(namebuf)) |
| 2307 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2308 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2309 | return FAIL; |
| 2310 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2311 | vim_strncpy(namebuf, *arg, varlen); |
| 2312 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2313 | |
| 2314 | *arg = skipwhite(*arg + varlen + 1); |
| 2315 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2316 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2317 | |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2318 | is_autoload = vim_strchr(name, '#') != NULL; |
| 2319 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2320 | { |
| 2321 | int idx; |
| 2322 | |
| 2323 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2324 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2325 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2326 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2327 | else |
| 2328 | semsg(_(e_unknownfunc), namebuf); |
| 2329 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2330 | } |
| 2331 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2332 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2333 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2334 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2335 | { |
| 2336 | res = generate_CALL(cctx, ufunc, argcount); |
| 2337 | goto theend; |
| 2338 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2339 | |
| 2340 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2341 | // 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] | 2342 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2343 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2344 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2345 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2346 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2347 | garray_T *stack = &cctx->ctx_type_stack; |
| 2348 | type_T *type; |
| 2349 | |
| 2350 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2351 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2352 | goto theend; |
| 2353 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2354 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2355 | // 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] | 2356 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2357 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2358 | res = generate_UCALL(cctx, name, argcount); |
| 2359 | else |
| 2360 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2361 | |
| 2362 | theend: |
| 2363 | vim_free(tofree); |
| 2364 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2368 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2369 | |
| 2370 | /* |
| 2371 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2372 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2373 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2374 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2375 | * valid name. |
| 2376 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2377 | static char_u * |
| 2378 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2379 | { |
| 2380 | char_u *p; |
| 2381 | |
| 2382 | // Quick check for valid starting character. |
| 2383 | if (!eval_isnamec1(*arg)) |
| 2384 | return arg; |
| 2385 | |
| 2386 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2387 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2388 | // and can be used in slice "[n:]". |
| 2389 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2390 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2391 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2392 | break; |
| 2393 | return p; |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * 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] | 2398 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2399 | */ |
| 2400 | char_u * |
| 2401 | to_name_const_end(char_u *arg) |
| 2402 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2403 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2404 | typval_T rettv; |
| 2405 | |
| 2406 | if (p == arg && *arg == '[') |
| 2407 | { |
| 2408 | |
| 2409 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2410 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2411 | p = arg; |
| 2412 | } |
| 2413 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2414 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2415 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2416 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2417 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2418 | p = arg; |
| 2419 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2420 | |
| 2421 | return p; |
| 2422 | } |
| 2423 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2424 | /* |
| 2425 | * parse a list: [expr, expr] |
| 2426 | * "*arg" points to the '['. |
| 2427 | */ |
| 2428 | static int |
| 2429 | compile_list(char_u **arg, cctx_T *cctx) |
| 2430 | { |
| 2431 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2432 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2433 | int count = 0; |
| 2434 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2435 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2436 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2437 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2438 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2439 | semsg(_(e_list_end), *arg); |
| 2440 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2441 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2442 | if (*p == ',') |
| 2443 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2444 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2445 | return FAIL; |
| 2446 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2447 | if (*p == ']') |
| 2448 | { |
| 2449 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2450 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2451 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2452 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2453 | break; |
| 2454 | ++count; |
| 2455 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2456 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2457 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2458 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2459 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2460 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2461 | return FAIL; |
| 2462 | } |
| 2463 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2464 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2465 | p = skipwhite(p); |
| 2466 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2467 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2468 | |
| 2469 | generate_NEWLIST(cctx, count); |
| 2470 | return OK; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * parse a lambda: {arg, arg -> expr} |
| 2475 | * "*arg" points to the '{'. |
| 2476 | */ |
| 2477 | static int |
| 2478 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2479 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2480 | typval_T rettv; |
| 2481 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2482 | evalarg_T evalarg; |
| 2483 | |
| 2484 | CLEAR_FIELD(evalarg); |
| 2485 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2486 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2487 | |
| 2488 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2489 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2490 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2491 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2492 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2493 | ++ufunc->uf_refcount; |
| 2494 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2495 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2496 | |
| 2497 | // The function will have one line: "return {expr}". |
| 2498 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2499 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2500 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2501 | clear_evalarg(&evalarg, NULL); |
| 2502 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2503 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2504 | { |
| 2505 | // The return type will now be known. |
| 2506 | set_function_type(ufunc); |
| 2507 | |
| 2508 | return generate_FUNCREF(cctx, ufunc); |
| 2509 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2510 | |
| 2511 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2512 | return FAIL; |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | * Compile a lamda call: expr->{lambda}(args) |
| 2517 | * "arg" points to the "{". |
| 2518 | */ |
| 2519 | static int |
| 2520 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2521 | { |
| 2522 | ufunc_T *ufunc; |
| 2523 | typval_T rettv; |
| 2524 | int argcount = 1; |
| 2525 | int ret = FAIL; |
| 2526 | |
| 2527 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2528 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2529 | return FAIL; |
| 2530 | |
| 2531 | if (**arg != '(') |
| 2532 | { |
| 2533 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2534 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2535 | else |
| 2536 | semsg(_(e_missing_paren), "lambda"); |
| 2537 | clear_tv(&rettv); |
| 2538 | return FAIL; |
| 2539 | } |
| 2540 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2541 | ufunc = rettv.vval.v_partial->pt_func; |
| 2542 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2543 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2544 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2545 | |
| 2546 | // The function will have one line: "return {expr}". |
| 2547 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2548 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2549 | |
| 2550 | // compile the arguments |
| 2551 | *arg = skipwhite(*arg + 1); |
| 2552 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2553 | // call the compiled function |
| 2554 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2555 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2556 | if (ret == FAIL) |
| 2557 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2558 | return ret; |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * parse a dict: {'key': val} or #{key: val} |
| 2563 | * "*arg" points to the '{'. |
| 2564 | */ |
| 2565 | static int |
| 2566 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2567 | { |
| 2568 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2569 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2570 | int count = 0; |
| 2571 | dict_T *d = dict_alloc(); |
| 2572 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2573 | char_u *whitep = *arg; |
| 2574 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2575 | |
| 2576 | if (d == NULL) |
| 2577 | return FAIL; |
| 2578 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2579 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2580 | { |
| 2581 | char_u *key = NULL; |
| 2582 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2583 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2584 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2585 | *arg = NULL; |
| 2586 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | if (**arg == '}') |
| 2590 | break; |
| 2591 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2592 | if (literal) |
| 2593 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2594 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2595 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2596 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2597 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2598 | semsg(_(e_invalid_key_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2599 | return FAIL; |
| 2600 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2601 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2602 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2603 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2604 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2605 | } |
| 2606 | else |
| 2607 | { |
| 2608 | isn_T *isn; |
| 2609 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2610 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2611 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2612 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2613 | if (isn->isn_type == ISN_PUSHS) |
| 2614 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2615 | else |
| 2616 | { |
| 2617 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2618 | [stack->ga_len - 1]; |
| 2619 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2620 | return FAIL; |
| 2621 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | // Check for duplicate keys, if using string keys. |
| 2625 | if (key != NULL) |
| 2626 | { |
| 2627 | item = dict_find(d, key, -1); |
| 2628 | if (item != NULL) |
| 2629 | { |
| 2630 | semsg(_(e_duplicate_key), key); |
| 2631 | goto failret; |
| 2632 | } |
| 2633 | item = dictitem_alloc(key); |
| 2634 | if (item != NULL) |
| 2635 | { |
| 2636 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2637 | item->di_tv.v_lock = 0; |
| 2638 | if (dict_add(d, item) == FAIL) |
| 2639 | dictitem_free(item); |
| 2640 | } |
| 2641 | } |
| 2642 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2643 | if (**arg != ':') |
| 2644 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2645 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2646 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2647 | else |
| 2648 | semsg(_(e_missing_dict_colon), *arg); |
| 2649 | return FAIL; |
| 2650 | } |
| 2651 | whitep = *arg + 1; |
| 2652 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 2653 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2654 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2655 | return FAIL; |
| 2656 | } |
| 2657 | |
| 2658 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2659 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2660 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2661 | *arg = NULL; |
| 2662 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2663 | } |
| 2664 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2665 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2666 | return FAIL; |
| 2667 | ++count; |
| 2668 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2669 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2670 | *arg = skipwhite(*arg); |
| 2671 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2672 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2673 | *arg = NULL; |
| 2674 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2675 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2676 | if (**arg == '}') |
| 2677 | break; |
| 2678 | if (**arg != ',') |
| 2679 | { |
| 2680 | semsg(_(e_missing_dict_comma), *arg); |
| 2681 | goto failret; |
| 2682 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2683 | if (IS_WHITE_OR_NUL(*whitep)) |
| 2684 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2685 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2686 | return FAIL; |
| 2687 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2688 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2689 | *arg = skipwhite(*arg + 1); |
| 2690 | } |
| 2691 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2692 | *arg = *arg + 1; |
| 2693 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2694 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2695 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2696 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2697 | *arg += STRLEN(*arg); |
| 2698 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2699 | dict_unref(d); |
| 2700 | return generate_NEWDICT(cctx, count); |
| 2701 | |
| 2702 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2703 | if (*arg == NULL) |
| 2704 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2705 | dict_unref(d); |
| 2706 | return FAIL; |
| 2707 | } |
| 2708 | |
| 2709 | /* |
| 2710 | * Compile "&option". |
| 2711 | */ |
| 2712 | static int |
| 2713 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2714 | { |
| 2715 | typval_T rettv; |
| 2716 | char_u *start = *arg; |
| 2717 | int ret; |
| 2718 | |
| 2719 | // parse the option and get the current value to get the type. |
| 2720 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2721 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2722 | if (ret == OK) |
| 2723 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2724 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2725 | char_u *name = vim_strnsave(start, *arg - start); |
| 2726 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2727 | |
| 2728 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2729 | vim_free(name); |
| 2730 | } |
| 2731 | clear_tv(&rettv); |
| 2732 | |
| 2733 | return ret; |
| 2734 | } |
| 2735 | |
| 2736 | /* |
| 2737 | * Compile "$VAR". |
| 2738 | */ |
| 2739 | static int |
| 2740 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2741 | { |
| 2742 | char_u *start = *arg; |
| 2743 | int len; |
| 2744 | int ret; |
| 2745 | char_u *name; |
| 2746 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2747 | ++*arg; |
| 2748 | len = get_env_len(arg); |
| 2749 | if (len == 0) |
| 2750 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2751 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2752 | return FAIL; |
| 2753 | } |
| 2754 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2755 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2756 | name = vim_strnsave(start, len + 1); |
| 2757 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2758 | vim_free(name); |
| 2759 | return ret; |
| 2760 | } |
| 2761 | |
| 2762 | /* |
| 2763 | * Compile "@r". |
| 2764 | */ |
| 2765 | static int |
| 2766 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2767 | { |
| 2768 | int ret; |
| 2769 | |
| 2770 | ++*arg; |
| 2771 | if (**arg == NUL) |
| 2772 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2773 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2774 | return FAIL; |
| 2775 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2776 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2777 | { |
| 2778 | emsg_invreg(**arg); |
| 2779 | return FAIL; |
| 2780 | } |
| 2781 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2782 | ++*arg; |
| 2783 | return ret; |
| 2784 | } |
| 2785 | |
| 2786 | /* |
| 2787 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2788 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2789 | */ |
| 2790 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2791 | 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] | 2792 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2793 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2794 | |
| 2795 | // this works from end to start |
| 2796 | while (p > start) |
| 2797 | { |
| 2798 | --p; |
| 2799 | if (*p == '-' || *p == '+') |
| 2800 | { |
| 2801 | // only '-' has an effect, for '+' we only check the type |
| 2802 | #ifdef FEAT_FLOAT |
| 2803 | if (rettv->v_type == VAR_FLOAT) |
| 2804 | { |
| 2805 | if (*p == '-') |
| 2806 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2807 | } |
| 2808 | else |
| 2809 | #endif |
| 2810 | { |
| 2811 | varnumber_T val; |
| 2812 | int error = FALSE; |
| 2813 | |
| 2814 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2815 | // here |
| 2816 | if (check_not_string(rettv) == FAIL) |
| 2817 | return FAIL; |
| 2818 | val = tv_get_number_chk(rettv, &error); |
| 2819 | clear_tv(rettv); |
| 2820 | if (error) |
| 2821 | return FAIL; |
| 2822 | if (*p == '-') |
| 2823 | val = -val; |
| 2824 | rettv->v_type = VAR_NUMBER; |
| 2825 | rettv->vval.v_number = val; |
| 2826 | } |
| 2827 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2828 | else if (numeric_only) |
| 2829 | { |
| 2830 | ++p; |
| 2831 | break; |
| 2832 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2833 | else |
| 2834 | { |
| 2835 | int v = tv2bool(rettv); |
| 2836 | |
| 2837 | // '!' is permissive in the type. |
| 2838 | clear_tv(rettv); |
| 2839 | rettv->v_type = VAR_BOOL; |
| 2840 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2841 | } |
| 2842 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2843 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2844 | return OK; |
| 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * Recognize v: variables that are constants and set "rettv". |
| 2849 | */ |
| 2850 | static void |
| 2851 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2852 | { |
| 2853 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2854 | { |
| 2855 | rettv->v_type = VAR_BOOL; |
| 2856 | rettv->vval.v_number = VVAL_TRUE; |
| 2857 | *arg += 6; |
| 2858 | } |
| 2859 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2860 | { |
| 2861 | rettv->v_type = VAR_BOOL; |
| 2862 | rettv->vval.v_number = VVAL_FALSE; |
| 2863 | *arg += 7; |
| 2864 | } |
| 2865 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2866 | { |
| 2867 | rettv->v_type = VAR_SPECIAL; |
| 2868 | rettv->vval.v_number = VVAL_NULL; |
| 2869 | *arg += 6; |
| 2870 | } |
| 2871 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2872 | { |
| 2873 | rettv->v_type = VAR_SPECIAL; |
| 2874 | rettv->vval.v_number = VVAL_NONE; |
| 2875 | *arg += 6; |
| 2876 | } |
| 2877 | } |
| 2878 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2879 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2880 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2881 | { |
| 2882 | exptype_T type = EXPR_UNKNOWN; |
| 2883 | int i; |
| 2884 | |
| 2885 | switch (p[0]) |
| 2886 | { |
| 2887 | case '=': if (p[1] == '=') |
| 2888 | type = EXPR_EQUAL; |
| 2889 | else if (p[1] == '~') |
| 2890 | type = EXPR_MATCH; |
| 2891 | break; |
| 2892 | case '!': if (p[1] == '=') |
| 2893 | type = EXPR_NEQUAL; |
| 2894 | else if (p[1] == '~') |
| 2895 | type = EXPR_NOMATCH; |
| 2896 | break; |
| 2897 | case '>': if (p[1] != '=') |
| 2898 | { |
| 2899 | type = EXPR_GREATER; |
| 2900 | *len = 1; |
| 2901 | } |
| 2902 | else |
| 2903 | type = EXPR_GEQUAL; |
| 2904 | break; |
| 2905 | case '<': if (p[1] != '=') |
| 2906 | { |
| 2907 | type = EXPR_SMALLER; |
| 2908 | *len = 1; |
| 2909 | } |
| 2910 | else |
| 2911 | type = EXPR_SEQUAL; |
| 2912 | break; |
| 2913 | case 'i': if (p[1] == 's') |
| 2914 | { |
| 2915 | // "is" and "isnot"; but not a prefix of a name |
| 2916 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2917 | *len = 5; |
| 2918 | i = p[*len]; |
| 2919 | if (!isalnum(i) && i != '_') |
| 2920 | { |
| 2921 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2922 | *type_is = TRUE; |
| 2923 | } |
| 2924 | } |
| 2925 | break; |
| 2926 | } |
| 2927 | return type; |
| 2928 | } |
| 2929 | |
| 2930 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2931 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2932 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2933 | */ |
| 2934 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2935 | 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] | 2936 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2937 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | |
| 2939 | // this works from end to start |
| 2940 | while (p > start) |
| 2941 | { |
| 2942 | --p; |
| 2943 | if (*p == '-' || *p == '+') |
| 2944 | { |
| 2945 | int negate = *p == '-'; |
| 2946 | isn_T *isn; |
| 2947 | |
| 2948 | // TODO: check type |
| 2949 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2950 | { |
| 2951 | --p; |
| 2952 | if (*p == '-') |
| 2953 | negate = !negate; |
| 2954 | } |
| 2955 | // only '-' has an effect, for '+' we only check the type |
| 2956 | if (negate) |
| 2957 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 2958 | else |
| 2959 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 2960 | if (isn == NULL) |
| 2961 | return FAIL; |
| 2962 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2963 | else if (numeric_only) |
| 2964 | { |
| 2965 | ++p; |
| 2966 | break; |
| 2967 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2968 | else |
| 2969 | { |
| 2970 | int invert = TRUE; |
| 2971 | |
| 2972 | while (p > start && p[-1] == '!') |
| 2973 | { |
| 2974 | --p; |
| 2975 | invert = !invert; |
| 2976 | } |
| 2977 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 2978 | return FAIL; |
| 2979 | } |
| 2980 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2981 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2982 | return OK; |
| 2983 | } |
| 2984 | |
| 2985 | /* |
| 2986 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2987 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2988 | */ |
| 2989 | static int |
| 2990 | compile_subscript( |
| 2991 | char_u **arg, |
| 2992 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2993 | char_u *start_leader, |
| 2994 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2995 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2996 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2997 | char_u *name_start = *end_leader; |
| 2998 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2999 | for (;;) |
| 3000 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3001 | char_u *p = skipwhite(*arg); |
| 3002 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3003 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3004 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3005 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3006 | |
| 3007 | // If a following line starts with "->{" or "->X" advance to that |
| 3008 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3009 | // Also if a following line starts with ".x". |
| 3010 | if (next != NULL && |
| 3011 | ((next[0] == '-' && next[1] == '>' |
| 3012 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3013 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3014 | { |
| 3015 | next = next_line_from_context(cctx, TRUE); |
| 3016 | if (next == NULL) |
| 3017 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3018 | *arg = next; |
| 3019 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3023 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3024 | // not a function call. |
| 3025 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3026 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3027 | garray_T *stack = &cctx->ctx_type_stack; |
| 3028 | type_T *type; |
| 3029 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3030 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3031 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3032 | return FAIL; |
| 3033 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3034 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3035 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3036 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3037 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3038 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3039 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3040 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3041 | return FAIL; |
| 3042 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3043 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3044 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3045 | char_u *pstart = p; |
| 3046 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3047 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3048 | return FAIL; |
| 3049 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3050 | // something->method() |
| 3051 | // Apply the '!', '-' and '+' first: |
| 3052 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3053 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3054 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3055 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3056 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3057 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3058 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3059 | if (**arg == '{') |
| 3060 | { |
| 3061 | // lambda call: list->{lambda} |
| 3062 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3063 | return FAIL; |
| 3064 | } |
| 3065 | else |
| 3066 | { |
| 3067 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3068 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3069 | if (!eval_isnamec1(*p)) |
| 3070 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3071 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3072 | return FAIL; |
| 3073 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3074 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3075 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3076 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3077 | ; |
| 3078 | if (*p != '(') |
| 3079 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3080 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3081 | return FAIL; |
| 3082 | } |
| 3083 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3084 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | return FAIL; |
| 3086 | } |
| 3087 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3088 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3089 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3090 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3091 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3092 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3093 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3094 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3095 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3096 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3097 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3098 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3099 | // TODO: blob index |
| 3100 | // TODO: more arguments |
| 3101 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3102 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3103 | return FAIL; |
| 3104 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3105 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3106 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3107 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3108 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3109 | if (**arg == ':') |
| 3110 | // missing first index is equal to zero |
| 3111 | generate_PUSHNR(cctx, 0); |
| 3112 | else |
| 3113 | { |
| 3114 | if (compile_expr0(arg, cctx) == FAIL) |
| 3115 | return FAIL; |
| 3116 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3117 | return FAIL; |
| 3118 | *arg = skipwhite(*arg); |
| 3119 | } |
| 3120 | if (**arg == ':') |
| 3121 | { |
| 3122 | *arg = skipwhite(*arg + 1); |
| 3123 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3124 | return FAIL; |
| 3125 | if (**arg == ']') |
| 3126 | // missing second index is equal to end of string |
| 3127 | generate_PUSHNR(cctx, -1); |
| 3128 | else |
| 3129 | { |
| 3130 | if (compile_expr0(arg, cctx) == FAIL) |
| 3131 | return FAIL; |
| 3132 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3133 | return FAIL; |
| 3134 | *arg = skipwhite(*arg); |
| 3135 | } |
| 3136 | is_slice = TRUE; |
| 3137 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3138 | |
| 3139 | if (**arg != ']') |
| 3140 | { |
| 3141 | emsg(_(e_missbrac)); |
| 3142 | return FAIL; |
| 3143 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3144 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3145 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3146 | // We can index a list and a dict. If we don't know the type |
| 3147 | // we can use the index value type. |
| 3148 | // TODO: If we don't know use an instruction to figure it out at |
| 3149 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3150 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3151 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3152 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3153 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3154 | // If the index is a string, the variable must be a Dict. |
| 3155 | if (*typep == &t_any && valtype == &t_string) |
| 3156 | vtype = VAR_DICT; |
| 3157 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3158 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3159 | if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL) |
| 3160 | return FAIL; |
| 3161 | if (is_slice) |
| 3162 | { |
| 3163 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 3164 | if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL) |
| 3165 | return FAIL; |
| 3166 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3167 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3168 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3169 | if (vtype == VAR_DICT) |
| 3170 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3171 | if (is_slice) |
| 3172 | { |
| 3173 | emsg(_(e_cannot_slice_dictionary)); |
| 3174 | return FAIL; |
| 3175 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3176 | if ((*typep)->tt_type == VAR_DICT) |
| 3177 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3178 | else |
| 3179 | { |
| 3180 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3181 | return FAIL; |
| 3182 | *typep = &t_any; |
| 3183 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3184 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3185 | return FAIL; |
| 3186 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3187 | return FAIL; |
| 3188 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3189 | else if (vtype == VAR_STRING) |
| 3190 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3191 | *typep = &t_string; |
| 3192 | if ((is_slice |
| 3193 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3194 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3195 | return FAIL; |
| 3196 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3197 | else if (vtype == VAR_BLOB) |
| 3198 | { |
| 3199 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3200 | return FAIL; |
| 3201 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3202 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3203 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3204 | if (is_slice) |
| 3205 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3206 | if (generate_instr_drop(cctx, |
| 3207 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3208 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3209 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3210 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3211 | else |
| 3212 | { |
| 3213 | if ((*typep)->tt_type == VAR_LIST) |
| 3214 | *typep = (*typep)->tt_member; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3215 | if (generate_instr_drop(cctx, |
| 3216 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3217 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3218 | return FAIL; |
| 3219 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3220 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3221 | else |
| 3222 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3223 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3224 | return FAIL; |
| 3225 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3226 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3227 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3228 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3229 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3230 | return FAIL; |
| 3231 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3232 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3233 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3234 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3235 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3236 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3237 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3238 | while (eval_isnamec(*p)) |
| 3239 | MB_PTR_ADV(p); |
| 3240 | if (p == *arg) |
| 3241 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3242 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3243 | return FAIL; |
| 3244 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3245 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3246 | return FAIL; |
| 3247 | *arg = p; |
| 3248 | } |
| 3249 | else |
| 3250 | break; |
| 3251 | } |
| 3252 | |
| 3253 | // TODO - see handle_subscript(): |
| 3254 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3255 | // Don't do this when "Func" is already a partial that was bound |
| 3256 | // explicitly (pt_auto is FALSE). |
| 3257 | |
| 3258 | return OK; |
| 3259 | } |
| 3260 | |
| 3261 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3262 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3263 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3264 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3265 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3266 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3267 | * |
| 3268 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3269 | */ |
| 3270 | |
| 3271 | /* |
| 3272 | * number number constant |
| 3273 | * 0zFFFFFFFF Blob constant |
| 3274 | * "string" string constant |
| 3275 | * 'string' literal string constant |
| 3276 | * &option-name option value |
| 3277 | * @r register contents |
| 3278 | * identifier variable value |
| 3279 | * function() function call |
| 3280 | * $VAR environment variable |
| 3281 | * (expression) nested expression |
| 3282 | * [expr, expr] List |
| 3283 | * {key: val, key: val} Dictionary |
| 3284 | * #{key: val, key: val} Dictionary with literal keys |
| 3285 | * |
| 3286 | * Also handle: |
| 3287 | * ! in front logical NOT |
| 3288 | * - in front unary minus |
| 3289 | * + in front unary plus (ignored) |
| 3290 | * trailing (arg) funcref/partial call |
| 3291 | * trailing [] subscript in String or List |
| 3292 | * trailing .name entry in Dictionary |
| 3293 | * trailing ->name() method call |
| 3294 | */ |
| 3295 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3296 | compile_expr7( |
| 3297 | char_u **arg, |
| 3298 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3299 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3300 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3301 | char_u *start_leader, *end_leader; |
| 3302 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3303 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3304 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3305 | |
| 3306 | /* |
| 3307 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3308 | */ |
| 3309 | start_leader = *arg; |
| 3310 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3311 | *arg = skipwhite(*arg + 1); |
| 3312 | end_leader = *arg; |
| 3313 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3314 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3315 | switch (**arg) |
| 3316 | { |
| 3317 | /* |
| 3318 | * Number constant. |
| 3319 | */ |
| 3320 | case '0': // also for blob starting with 0z |
| 3321 | case '1': |
| 3322 | case '2': |
| 3323 | case '3': |
| 3324 | case '4': |
| 3325 | case '5': |
| 3326 | case '6': |
| 3327 | case '7': |
| 3328 | case '8': |
| 3329 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3330 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3331 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3332 | // Apply "-" and "+" just before the number now, right to |
| 3333 | // left. Matters especially when "->" follows. Stops at |
| 3334 | // '!'. |
| 3335 | if (apply_leader(rettv, TRUE, |
| 3336 | start_leader, &end_leader) == FAIL) |
| 3337 | { |
| 3338 | clear_tv(rettv); |
| 3339 | return FAIL; |
| 3340 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3341 | break; |
| 3342 | |
| 3343 | /* |
| 3344 | * String constant: "string". |
| 3345 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3346 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3347 | return FAIL; |
| 3348 | break; |
| 3349 | |
| 3350 | /* |
| 3351 | * Literal string constant: 'str''ing'. |
| 3352 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3353 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3354 | return FAIL; |
| 3355 | break; |
| 3356 | |
| 3357 | /* |
| 3358 | * Constant Vim variable. |
| 3359 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3360 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3361 | ret = NOTDONE; |
| 3362 | break; |
| 3363 | |
| 3364 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3365 | * "true" constant |
| 3366 | */ |
| 3367 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3368 | && !eval_isnamec((*arg)[4])) |
| 3369 | { |
| 3370 | *arg += 4; |
| 3371 | rettv->v_type = VAR_BOOL; |
| 3372 | rettv->vval.v_number = VVAL_TRUE; |
| 3373 | } |
| 3374 | else |
| 3375 | ret = NOTDONE; |
| 3376 | break; |
| 3377 | |
| 3378 | /* |
| 3379 | * "false" constant |
| 3380 | */ |
| 3381 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3382 | && !eval_isnamec((*arg)[5])) |
| 3383 | { |
| 3384 | *arg += 5; |
| 3385 | rettv->v_type = VAR_BOOL; |
| 3386 | rettv->vval.v_number = VVAL_FALSE; |
| 3387 | } |
| 3388 | else |
| 3389 | ret = NOTDONE; |
| 3390 | break; |
| 3391 | |
| 3392 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3393 | * List: [expr, expr] |
| 3394 | */ |
| 3395 | case '[': ret = compile_list(arg, cctx); |
| 3396 | break; |
| 3397 | |
| 3398 | /* |
| 3399 | * Dictionary: #{key: val, key: val} |
| 3400 | */ |
| 3401 | case '#': if ((*arg)[1] == '{') |
| 3402 | { |
| 3403 | ++*arg; |
| 3404 | ret = compile_dict(arg, cctx, TRUE); |
| 3405 | } |
| 3406 | else |
| 3407 | ret = NOTDONE; |
| 3408 | break; |
| 3409 | |
| 3410 | /* |
| 3411 | * Lambda: {arg, arg -> expr} |
| 3412 | * Dictionary: {'key': val, 'key': val} |
| 3413 | */ |
| 3414 | case '{': { |
| 3415 | char_u *start = skipwhite(*arg + 1); |
| 3416 | |
| 3417 | // Find out what comes after the arguments. |
| 3418 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3419 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3420 | if (ret != FAIL && *start == '>') |
| 3421 | ret = compile_lambda(arg, cctx); |
| 3422 | else |
| 3423 | ret = compile_dict(arg, cctx, FALSE); |
| 3424 | } |
| 3425 | break; |
| 3426 | |
| 3427 | /* |
| 3428 | * Option value: &name |
| 3429 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3430 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3431 | return FAIL; |
| 3432 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3433 | break; |
| 3434 | |
| 3435 | /* |
| 3436 | * Environment variable: $VAR. |
| 3437 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3438 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3439 | return FAIL; |
| 3440 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3441 | break; |
| 3442 | |
| 3443 | /* |
| 3444 | * Register contents: @r. |
| 3445 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3446 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3447 | return FAIL; |
| 3448 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3449 | break; |
| 3450 | /* |
| 3451 | * nested expression: (expression). |
| 3452 | */ |
| 3453 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3454 | |
| 3455 | // recursive! |
| 3456 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3457 | { |
| 3458 | ret = compile_expr1(arg, cctx, ppconst); |
| 3459 | } |
| 3460 | else |
| 3461 | { |
| 3462 | // Not enough space in ppconst, flush constants. |
| 3463 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3464 | return FAIL; |
| 3465 | ret = compile_expr0(arg, cctx); |
| 3466 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3467 | *arg = skipwhite(*arg); |
| 3468 | if (**arg == ')') |
| 3469 | ++*arg; |
| 3470 | else if (ret == OK) |
| 3471 | { |
| 3472 | emsg(_(e_missing_close)); |
| 3473 | ret = FAIL; |
| 3474 | } |
| 3475 | break; |
| 3476 | |
| 3477 | default: ret = NOTDONE; |
| 3478 | break; |
| 3479 | } |
| 3480 | if (ret == FAIL) |
| 3481 | return FAIL; |
| 3482 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3483 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3485 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3486 | clear_tv(rettv); |
| 3487 | else |
| 3488 | // A constant expression can possibly be handled compile time, |
| 3489 | // return the value instead of generating code. |
| 3490 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3491 | } |
| 3492 | else if (ret == NOTDONE) |
| 3493 | { |
| 3494 | char_u *p; |
| 3495 | int r; |
| 3496 | |
| 3497 | if (!eval_isnamec1(**arg)) |
| 3498 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3499 | semsg(_(e_name_expected), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3500 | return FAIL; |
| 3501 | } |
| 3502 | |
| 3503 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3504 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3505 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3506 | { |
| 3507 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3508 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3509 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3510 | { |
| 3511 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3512 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3513 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3514 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3515 | if (r == FAIL) |
| 3516 | return FAIL; |
| 3517 | } |
| 3518 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3519 | // Handle following "[]", ".member", etc. |
| 3520 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3521 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3522 | ppconst) == FAIL) |
| 3523 | return FAIL; |
| 3524 | if (ppconst->pp_used > 0) |
| 3525 | { |
| 3526 | // apply the '!', '-' and '+' before the constant |
| 3527 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3528 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3529 | return FAIL; |
| 3530 | return OK; |
| 3531 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3532 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3533 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3534 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3538 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3539 | */ |
| 3540 | void |
| 3541 | error_white_both(char_u *op, int len) |
| 3542 | { |
| 3543 | char_u buf[10]; |
| 3544 | |
| 3545 | vim_strncpy(buf, op, len); |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3546 | semsg(_(e_white_space_required_before_and_after_str), buf); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3550 | * <type>expr7: runtime type check / conversion |
| 3551 | */ |
| 3552 | static int |
| 3553 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3554 | { |
| 3555 | type_T *want_type = NULL; |
| 3556 | |
| 3557 | // Recognize <type> |
| 3558 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3559 | { |
| 3560 | int called_emsg_before = called_emsg; |
| 3561 | |
| 3562 | ++*arg; |
| 3563 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3564 | if (called_emsg != called_emsg_before) |
| 3565 | return FAIL; |
| 3566 | |
| 3567 | if (**arg != '>') |
| 3568 | { |
| 3569 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3570 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3571 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3572 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3573 | return FAIL; |
| 3574 | } |
| 3575 | ++*arg; |
| 3576 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3577 | return FAIL; |
| 3578 | } |
| 3579 | |
| 3580 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3581 | return FAIL; |
| 3582 | |
| 3583 | if (want_type != NULL) |
| 3584 | { |
| 3585 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3586 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3587 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3588 | generate_ppconst(cctx, ppconst); |
| 3589 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 3590 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3591 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3592 | if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL) |
| 3593 | return FAIL; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | return OK; |
| 3598 | } |
| 3599 | |
| 3600 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3601 | * * number multiplication |
| 3602 | * / number division |
| 3603 | * % number modulo |
| 3604 | */ |
| 3605 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3606 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3607 | { |
| 3608 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3609 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3610 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3611 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3612 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3613 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3614 | return FAIL; |
| 3615 | |
| 3616 | /* |
| 3617 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3618 | */ |
| 3619 | for (;;) |
| 3620 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3621 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3622 | if (*op != '*' && *op != '/' && *op != '%') |
| 3623 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3624 | if (next != NULL) |
| 3625 | { |
| 3626 | *arg = next_line_from_context(cctx, TRUE); |
| 3627 | op = skipwhite(*arg); |
| 3628 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3629 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3630 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3631 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3632 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3633 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3634 | } |
| 3635 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3636 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3637 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3638 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3639 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3640 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3641 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3642 | |
| 3643 | if (ppconst->pp_used == ppconst_used + 2 |
| 3644 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3645 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3646 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3647 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3648 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3649 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3650 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3651 | // both are numbers: compute the result |
| 3652 | switch (*op) |
| 3653 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3654 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3655 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3656 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3657 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3658 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3659 | break; |
| 3660 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3661 | tv1->vval.v_number = res; |
| 3662 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3663 | } |
| 3664 | else |
| 3665 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3666 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3667 | generate_two_op(cctx, op); |
| 3668 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | return OK; |
| 3672 | } |
| 3673 | |
| 3674 | /* |
| 3675 | * + number addition |
| 3676 | * - number subtraction |
| 3677 | * .. string concatenation |
| 3678 | */ |
| 3679 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3680 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3681 | { |
| 3682 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3683 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3684 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3685 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3686 | |
| 3687 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3688 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3689 | return FAIL; |
| 3690 | |
| 3691 | /* |
| 3692 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3693 | */ |
| 3694 | for (;;) |
| 3695 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3696 | op = may_peek_next_line(cctx, *arg, &next); |
| 3697 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3698 | break; |
| 3699 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3700 | if (next != NULL) |
| 3701 | { |
| 3702 | *arg = next_line_from_context(cctx, TRUE); |
| 3703 | op = skipwhite(*arg); |
| 3704 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3705 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3706 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3707 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3708 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3709 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3713 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3714 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3715 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3716 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3717 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3718 | return FAIL; |
| 3719 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3720 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3721 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3722 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3723 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3724 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3725 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3726 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3727 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3728 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3729 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3730 | // concat/subtract/add constant numbers |
| 3731 | if (*op == '+') |
| 3732 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3733 | else if (*op == '-') |
| 3734 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3735 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3736 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3737 | // concatenate constant strings |
| 3738 | char_u *s1 = tv1->vval.v_string; |
| 3739 | char_u *s2 = tv2->vval.v_string; |
| 3740 | size_t len1 = STRLEN(s1); |
| 3741 | |
| 3742 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3743 | if (tv1->vval.v_string == NULL) |
| 3744 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3745 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3746 | return FAIL; |
| 3747 | } |
| 3748 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3749 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3750 | vim_free(s1); |
| 3751 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3752 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3753 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3754 | } |
| 3755 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3756 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3757 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3758 | if (*op == '.') |
| 3759 | { |
| 3760 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3761 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3762 | return FAIL; |
| 3763 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3764 | } |
| 3765 | else |
| 3766 | generate_two_op(cctx, op); |
| 3767 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | return OK; |
| 3771 | } |
| 3772 | |
| 3773 | /* |
| 3774 | * expr5a == expr5b |
| 3775 | * expr5a =~ expr5b |
| 3776 | * expr5a != expr5b |
| 3777 | * expr5a !~ expr5b |
| 3778 | * expr5a > expr5b |
| 3779 | * expr5a >= expr5b |
| 3780 | * expr5a < expr5b |
| 3781 | * expr5a <= expr5b |
| 3782 | * expr5a is expr5b |
| 3783 | * expr5a isnot expr5b |
| 3784 | * |
| 3785 | * Produces instructions: |
| 3786 | * EVAL expr5a Push result of "expr5a" |
| 3787 | * EVAL expr5b Push result of "expr5b" |
| 3788 | * COMPARE one of the compare instructions |
| 3789 | */ |
| 3790 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3791 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | { |
| 3793 | exptype_T type = EXPR_UNKNOWN; |
| 3794 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3795 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3796 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3797 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3798 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3799 | |
| 3800 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3801 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3802 | return FAIL; |
| 3803 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3804 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3805 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3806 | |
| 3807 | /* |
| 3808 | * If there is a comparative operator, use it. |
| 3809 | */ |
| 3810 | if (type != EXPR_UNKNOWN) |
| 3811 | { |
| 3812 | int ic = FALSE; // Default: do not ignore case |
| 3813 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3814 | if (next != NULL) |
| 3815 | { |
| 3816 | *arg = next_line_from_context(cctx, TRUE); |
| 3817 | p = skipwhite(*arg); |
| 3818 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3819 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3820 | { |
| 3821 | semsg(_(e_invexpr2), *arg); |
| 3822 | return FAIL; |
| 3823 | } |
| 3824 | // extra question mark appended: ignore case |
| 3825 | if (p[len] == '?') |
| 3826 | { |
| 3827 | ic = TRUE; |
| 3828 | ++len; |
| 3829 | } |
| 3830 | // extra '#' appended: match case (ignored) |
| 3831 | else if (p[len] == '#') |
| 3832 | ++len; |
| 3833 | // nothing appended: match case |
| 3834 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3835 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3836 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3837 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3838 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | // get the second variable |
| 3842 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3843 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3844 | return FAIL; |
| 3845 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3846 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3847 | return FAIL; |
| 3848 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3849 | if (ppconst->pp_used == ppconst_used + 2) |
| 3850 | { |
| 3851 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3852 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3853 | int ret; |
| 3854 | |
| 3855 | // Both sides are a constant, compute the result now. |
| 3856 | // First check for a valid combination of types, this is more |
| 3857 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3858 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3859 | ret = FAIL; |
| 3860 | else |
| 3861 | { |
| 3862 | ret = typval_compare(tv1, tv2, type, ic); |
| 3863 | tv1->v_type = VAR_BOOL; |
| 3864 | tv1->vval.v_number = tv1->vval.v_number |
| 3865 | ? VVAL_TRUE : VVAL_FALSE; |
| 3866 | clear_tv(tv2); |
| 3867 | --ppconst->pp_used; |
| 3868 | } |
| 3869 | return ret; |
| 3870 | } |
| 3871 | |
| 3872 | generate_ppconst(cctx, ppconst); |
| 3873 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | return OK; |
| 3877 | } |
| 3878 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3879 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3880 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | /* |
| 3882 | * Compile || or &&. |
| 3883 | */ |
| 3884 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3885 | compile_and_or( |
| 3886 | char_u **arg, |
| 3887 | cctx_T *cctx, |
| 3888 | char *op, |
| 3889 | ppconst_T *ppconst, |
| 3890 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3891 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3892 | char_u *next; |
| 3893 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3894 | int opchar = *op; |
| 3895 | |
| 3896 | if (p[0] == opchar && p[1] == opchar) |
| 3897 | { |
| 3898 | garray_T *instr = &cctx->ctx_instr; |
| 3899 | garray_T end_ga; |
| 3900 | |
| 3901 | /* |
| 3902 | * Repeat until there is no following "||" or "&&" |
| 3903 | */ |
| 3904 | ga_init2(&end_ga, sizeof(int), 10); |
| 3905 | while (p[0] == opchar && p[1] == opchar) |
| 3906 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3907 | if (next != NULL) |
| 3908 | { |
| 3909 | *arg = next_line_from_context(cctx, TRUE); |
| 3910 | p = skipwhite(*arg); |
| 3911 | } |
| 3912 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3913 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3914 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3915 | semsg(_(e_white_space_required_before_and_after_str), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3916 | return FAIL; |
| 3917 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3918 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3919 | // TODO: use ppconst if the value is a constant |
| 3920 | generate_ppconst(cctx, ppconst); |
| 3921 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3922 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3923 | { |
| 3924 | ga_clear(&end_ga); |
| 3925 | return FAIL; |
| 3926 | } |
| 3927 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3928 | ++end_ga.ga_len; |
| 3929 | generate_JUMP(cctx, opchar == '|' |
| 3930 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3931 | |
| 3932 | // eval the next expression |
| 3933 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3934 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3935 | return FAIL; |
| 3936 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3937 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3938 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3939 | { |
| 3940 | ga_clear(&end_ga); |
| 3941 | return FAIL; |
| 3942 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3943 | |
| 3944 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3945 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3946 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3947 | |
| 3948 | // Fill in the end label in all jumps. |
| 3949 | while (end_ga.ga_len > 0) |
| 3950 | { |
| 3951 | isn_T *isn; |
| 3952 | |
| 3953 | --end_ga.ga_len; |
| 3954 | isn = ((isn_T *)instr->ga_data) |
| 3955 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3956 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3957 | } |
| 3958 | ga_clear(&end_ga); |
| 3959 | } |
| 3960 | |
| 3961 | return OK; |
| 3962 | } |
| 3963 | |
| 3964 | /* |
| 3965 | * expr4a && expr4a && expr4a logical AND |
| 3966 | * |
| 3967 | * Produces instructions: |
| 3968 | * EVAL expr4a Push result of "expr4a" |
| 3969 | * JUMP_AND_KEEP_IF_FALSE end |
| 3970 | * EVAL expr4b Push result of "expr4b" |
| 3971 | * JUMP_AND_KEEP_IF_FALSE end |
| 3972 | * EVAL expr4c Push result of "expr4c" |
| 3973 | * end: |
| 3974 | */ |
| 3975 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3976 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3977 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3978 | int ppconst_used = ppconst->pp_used; |
| 3979 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3980 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3981 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3982 | return FAIL; |
| 3983 | |
| 3984 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3985 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | /* |
| 3989 | * expr3a || expr3b || expr3c logical OR |
| 3990 | * |
| 3991 | * Produces instructions: |
| 3992 | * EVAL expr3a Push result of "expr3a" |
| 3993 | * JUMP_AND_KEEP_IF_TRUE end |
| 3994 | * EVAL expr3b Push result of "expr3b" |
| 3995 | * JUMP_AND_KEEP_IF_TRUE end |
| 3996 | * EVAL expr3c Push result of "expr3c" |
| 3997 | * end: |
| 3998 | */ |
| 3999 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4000 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4001 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4002 | int ppconst_used = ppconst->pp_used; |
| 4003 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4004 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4005 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4006 | return FAIL; |
| 4007 | |
| 4008 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4009 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | /* |
| 4013 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4014 | * |
| 4015 | * Produces instructions: |
| 4016 | * EVAL expr2 Push result of "expr" |
| 4017 | * JUMP_IF_FALSE alt jump if false |
| 4018 | * EVAL expr1a |
| 4019 | * JUMP_ALWAYS end |
| 4020 | * alt: EVAL expr1b |
| 4021 | * end: |
| 4022 | */ |
| 4023 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4024 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4025 | { |
| 4026 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4027 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4028 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4029 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4030 | // Ignore all kinds of errors when not producing code. |
| 4031 | if (cctx->ctx_skip == SKIP_YES) |
| 4032 | { |
| 4033 | skip_expr(arg); |
| 4034 | return OK; |
| 4035 | } |
| 4036 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4037 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4038 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4039 | return FAIL; |
| 4040 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4041 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4042 | if (*p == '?') |
| 4043 | { |
| 4044 | garray_T *instr = &cctx->ctx_instr; |
| 4045 | garray_T *stack = &cctx->ctx_type_stack; |
| 4046 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4047 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4048 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4049 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4050 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4051 | int has_const_expr = FALSE; |
| 4052 | int const_value = FALSE; |
| 4053 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4054 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4055 | if (next != NULL) |
| 4056 | { |
| 4057 | *arg = next_line_from_context(cctx, TRUE); |
| 4058 | p = skipwhite(*arg); |
| 4059 | } |
| 4060 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4061 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4062 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4063 | semsg(_(e_white_space_required_before_and_after_str), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4064 | return FAIL; |
| 4065 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4066 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4067 | if (ppconst->pp_used == ppconst_used + 1) |
| 4068 | { |
| 4069 | // the condition is a constant, we know whether the ? or the : |
| 4070 | // expression is to be evaluated. |
| 4071 | has_const_expr = TRUE; |
| 4072 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4073 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4074 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4075 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4076 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4077 | } |
| 4078 | else |
| 4079 | { |
| 4080 | generate_ppconst(cctx, ppconst); |
| 4081 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4082 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4083 | |
| 4084 | // evaluate the second expression; any type is accepted |
| 4085 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4086 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4087 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4088 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4089 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4090 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4091 | if (!has_const_expr) |
| 4092 | { |
| 4093 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4094 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4095 | // remember the type and drop it |
| 4096 | --stack->ga_len; |
| 4097 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4098 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4099 | end_idx = instr->ga_len; |
| 4100 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4101 | |
| 4102 | // jump here from JUMP_IF_FALSE |
| 4103 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4104 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4105 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4106 | |
| 4107 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4108 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4109 | if (*p != ':') |
| 4110 | { |
| 4111 | emsg(_(e_missing_colon)); |
| 4112 | return FAIL; |
| 4113 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4114 | if (next != NULL) |
| 4115 | { |
| 4116 | *arg = next_line_from_context(cctx, TRUE); |
| 4117 | p = skipwhite(*arg); |
| 4118 | } |
| 4119 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4120 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4121 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4122 | semsg(_(e_white_space_required_before_and_after_str), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4123 | return FAIL; |
| 4124 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4125 | |
| 4126 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4127 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4128 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4129 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4130 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4131 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4132 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4133 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4134 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4135 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4136 | if (!has_const_expr) |
| 4137 | { |
| 4138 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4139 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4140 | // If the types differ, the result has a more generic type. |
| 4141 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4142 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4143 | |
| 4144 | // jump here from JUMP_ALWAYS |
| 4145 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4146 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4147 | } |
| 4148 | |
| 4149 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4150 | } |
| 4151 | return OK; |
| 4152 | } |
| 4153 | |
| 4154 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4155 | * Toplevel expression. |
| 4156 | */ |
| 4157 | static int |
| 4158 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4159 | { |
| 4160 | ppconst_T ppconst; |
| 4161 | |
| 4162 | CLEAR_FIELD(ppconst); |
| 4163 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4164 | { |
| 4165 | clear_ppconst(&ppconst); |
| 4166 | return FAIL; |
| 4167 | } |
| 4168 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4169 | return FAIL; |
| 4170 | return OK; |
| 4171 | } |
| 4172 | |
| 4173 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4174 | * compile "return [expr]" |
| 4175 | */ |
| 4176 | static char_u * |
| 4177 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4178 | { |
| 4179 | char_u *p = arg; |
| 4180 | garray_T *stack = &cctx->ctx_type_stack; |
| 4181 | type_T *stack_type; |
| 4182 | |
| 4183 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4184 | { |
| 4185 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4186 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4187 | return NULL; |
| 4188 | |
| 4189 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4190 | if (set_return_type) |
| 4191 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4192 | else |
| 4193 | { |
| 4194 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4195 | && stack_type->tt_type != VAR_VOID |
| 4196 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4197 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4198 | emsg(_(e_returning_value_in_function_without_return_type)); |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4199 | return NULL; |
| 4200 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4201 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4202 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4203 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4204 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | } |
| 4206 | else |
| 4207 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4208 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4209 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4210 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4211 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4212 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4213 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | return NULL; |
| 4215 | } |
| 4216 | |
| 4217 | // No argument, return zero. |
| 4218 | generate_PUSHNR(cctx, 0); |
| 4219 | } |
| 4220 | |
| 4221 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4222 | return NULL; |
| 4223 | |
| 4224 | // "return val | endif" is possible |
| 4225 | return skipwhite(p); |
| 4226 | } |
| 4227 | |
| 4228 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4229 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4230 | * Return a pointer to the line in allocated memory. |
| 4231 | * Return NULL for end-of-file or some error. |
| 4232 | */ |
| 4233 | static char_u * |
| 4234 | exarg_getline( |
| 4235 | int c UNUSED, |
| 4236 | void *cookie, |
| 4237 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4238 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4239 | { |
| 4240 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4241 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4242 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4243 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4244 | { |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4245 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4246 | return NULL; |
| 4247 | ++cctx->ctx_lnum; |
| 4248 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4249 | // Comment lines result in NULL pointers, skip them. |
| 4250 | if (p != NULL) |
| 4251 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4252 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | /* |
| 4256 | * Compile a nested :def command. |
| 4257 | */ |
| 4258 | static char_u * |
| 4259 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4260 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4261 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4262 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4263 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4264 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4265 | lvar_T *lvar; |
| 4266 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4267 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4268 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4269 | // Only g:Func() can use a namespace. |
| 4270 | if (name_start[1] == ':' && !is_global) |
| 4271 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4272 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4273 | return NULL; |
| 4274 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4275 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4276 | return NULL; |
| 4277 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4278 | eap->arg = name_end; |
| 4279 | eap->getline = exarg_getline; |
| 4280 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4281 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4282 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4283 | lambda_name = get_lambda_name(); |
| 4284 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4285 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4286 | if (ufunc == NULL) |
Bram Moolenaar | 7a3330f | 2020-08-27 23:57:57 +0200 | [diff] [blame] | 4287 | return eap->skip ? (char_u *)"" : NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4288 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4289 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4290 | return NULL; |
| 4291 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4292 | if (is_global) |
| 4293 | { |
| 4294 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4295 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4296 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4297 | if (func_name == NULL) |
| 4298 | r = FAIL; |
| 4299 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4300 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4301 | } |
| 4302 | else |
| 4303 | { |
| 4304 | // Define a local variable for the function reference. |
| 4305 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4306 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4307 | if (lvar == NULL) |
| 4308 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4309 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4310 | return NULL; |
| 4311 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4312 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4313 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4314 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4315 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4319 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4320 | */ |
| 4321 | int |
| 4322 | assignment_len(char_u *p, int *heredoc) |
| 4323 | { |
| 4324 | if (*p == '=') |
| 4325 | { |
| 4326 | if (p[1] == '<' && p[2] == '<') |
| 4327 | { |
| 4328 | *heredoc = TRUE; |
| 4329 | return 3; |
| 4330 | } |
| 4331 | return 1; |
| 4332 | } |
| 4333 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4334 | return 2; |
| 4335 | if (STRNCMP(p, "..=", 3) == 0) |
| 4336 | return 3; |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
| 4340 | // words that cannot be used as a variable |
| 4341 | static char *reserved[] = { |
| 4342 | "true", |
| 4343 | "false", |
| 4344 | NULL |
| 4345 | }; |
| 4346 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4347 | typedef enum { |
| 4348 | dest_local, |
| 4349 | dest_option, |
| 4350 | dest_env, |
| 4351 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4352 | dest_buffer, |
| 4353 | dest_window, |
| 4354 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4355 | dest_vimvar, |
| 4356 | dest_script, |
| 4357 | dest_reg, |
| 4358 | } assign_dest_T; |
| 4359 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4360 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4361 | * Generate the load instruction for "name". |
| 4362 | */ |
| 4363 | static void |
| 4364 | generate_loadvar( |
| 4365 | cctx_T *cctx, |
| 4366 | assign_dest_T dest, |
| 4367 | char_u *name, |
| 4368 | lvar_T *lvar, |
| 4369 | type_T *type) |
| 4370 | { |
| 4371 | switch (dest) |
| 4372 | { |
| 4373 | case dest_option: |
| 4374 | // TODO: check the option exists |
| 4375 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4376 | break; |
| 4377 | case dest_global: |
| 4378 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4379 | break; |
| 4380 | case dest_buffer: |
| 4381 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4382 | break; |
| 4383 | case dest_window: |
| 4384 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4385 | break; |
| 4386 | case dest_tab: |
| 4387 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4388 | break; |
| 4389 | case dest_script: |
| 4390 | compile_load_scriptvar(cctx, |
| 4391 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4392 | break; |
| 4393 | case dest_env: |
| 4394 | // Include $ in the name here |
| 4395 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4396 | break; |
| 4397 | case dest_reg: |
| 4398 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4399 | break; |
| 4400 | case dest_vimvar: |
| 4401 | generate_LOADV(cctx, name + 2, TRUE); |
| 4402 | break; |
| 4403 | case dest_local: |
| 4404 | if (lvar->lv_from_outer) |
| 4405 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4406 | NULL, type); |
| 4407 | else |
| 4408 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4409 | break; |
| 4410 | } |
| 4411 | } |
| 4412 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4413 | void |
| 4414 | vim9_declare_error(char_u *name) |
| 4415 | { |
| 4416 | char *scope = ""; |
| 4417 | |
| 4418 | switch (*name) |
| 4419 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4420 | case 'g': scope = _("global"); break; |
| 4421 | case 'b': scope = _("buffer"); break; |
| 4422 | case 'w': scope = _("window"); break; |
| 4423 | case 't': scope = _("tab"); break; |
| 4424 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4425 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4426 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4427 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4428 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4429 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4430 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4431 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4432 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4433 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4434 | } |
| 4435 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4436 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4437 | * Compile declaration and assignment: |
| 4438 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4439 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4440 | * Return NULL for an error. |
| 4441 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4442 | */ |
| 4443 | static char_u * |
| 4444 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4445 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4446 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4447 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4448 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4449 | char_u *ret = NULL; |
| 4450 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4451 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4452 | int scriptvar_sid = 0; |
| 4453 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4454 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4455 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4456 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4457 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4458 | int oplen = 0; |
| 4459 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4460 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4461 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4462 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4463 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4464 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4465 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4466 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4467 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4468 | if (p == NULL) |
| 4469 | return *arg == '[' ? arg : NULL; |
| 4470 | |
| 4471 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4472 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4473 | // TODO: should we allow this, and figure out type inference from list |
| 4474 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4475 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4476 | return NULL; |
| 4477 | } |
| 4478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4479 | sp = p; |
| 4480 | p = skipwhite(p); |
| 4481 | op = p; |
| 4482 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4483 | |
| 4484 | if (var_count > 0 && oplen == 0) |
| 4485 | // can be something like "[1, 2]->func()" |
| 4486 | return arg; |
| 4487 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4488 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4489 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4490 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4491 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4492 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4493 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4494 | if (heredoc) |
| 4495 | { |
| 4496 | list_T *l; |
| 4497 | listitem_T *li; |
| 4498 | |
| 4499 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4500 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4501 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4502 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4503 | |
| 4504 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4505 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4506 | { |
| 4507 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4508 | li->li_tv.vval.v_string = NULL; |
| 4509 | } |
| 4510 | generate_NEWLIST(cctx, l->lv_len); |
| 4511 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4512 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4513 | list_free(l); |
| 4514 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4515 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4516 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4517 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4518 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4519 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4520 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4521 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4522 | p = skipwhite(op + oplen); |
| 4523 | if (compile_expr0(&p, cctx) == FAIL) |
| 4524 | return NULL; |
| 4525 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4526 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4527 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4528 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4529 | type_T *stacktype; |
| 4530 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4531 | stacktype = stack->ga_len == 0 ? &t_void |
| 4532 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4533 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4534 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4535 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4536 | goto theend; |
| 4537 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4538 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4539 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4540 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4541 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4542 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | /* |
| 4547 | * Loop over variables in "[var, var] = expr". |
| 4548 | * For "var = expr" and "let var: type" this is done only once. |
| 4549 | */ |
| 4550 | if (var_count > 0) |
| 4551 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4552 | else |
| 4553 | var_start = arg; |
| 4554 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4555 | { |
| 4556 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4557 | size_t varlen; |
| 4558 | int new_local = FALSE; |
| 4559 | int opt_type; |
| 4560 | int opt_flags = 0; |
| 4561 | assign_dest_T dest = dest_local; |
| 4562 | int vimvaridx = -1; |
| 4563 | lvar_T *lvar = NULL; |
| 4564 | lvar_T arg_lvar; |
| 4565 | int has_type = FALSE; |
| 4566 | int has_index = FALSE; |
| 4567 | int instr_count = -1; |
| 4568 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4569 | if (*var_start == '@') |
| 4570 | p = var_start + 2; |
| 4571 | else |
| 4572 | { |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4573 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 4574 | p = skip_option_env_lead(var_start); |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4575 | p = to_name_end(p, TRUE); |
| 4576 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4577 | |
| 4578 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4579 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4580 | --var_end; |
| 4581 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4582 | --p; |
| 4583 | |
| 4584 | varlen = p - var_start; |
| 4585 | vim_free(name); |
| 4586 | name = vim_strnsave(var_start, varlen); |
| 4587 | if (name == NULL) |
| 4588 | return NULL; |
| 4589 | if (!heredoc) |
| 4590 | type = &t_any; |
| 4591 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4592 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4593 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4594 | int declare_error = FALSE; |
| 4595 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4596 | if (*var_start == '&') |
| 4597 | { |
| 4598 | int cc; |
| 4599 | long numval; |
| 4600 | |
| 4601 | dest = dest_option; |
| 4602 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4603 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4604 | emsg(_(e_const_option)); |
| 4605 | goto theend; |
| 4606 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4607 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4608 | p = var_start; |
| 4609 | p = find_option_end(&p, &opt_flags); |
| 4610 | if (p == NULL) |
| 4611 | { |
| 4612 | // cannot happen? |
| 4613 | emsg(_(e_letunexp)); |
| 4614 | goto theend; |
| 4615 | } |
| 4616 | cc = *p; |
| 4617 | *p = NUL; |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4618 | opt_type = get_option_value(skip_option_env_lead(var_start), |
| 4619 | &numval, NULL, opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4620 | *p = cc; |
| 4621 | if (opt_type == -3) |
| 4622 | { |
| 4623 | semsg(_(e_unknown_option), var_start); |
| 4624 | goto theend; |
| 4625 | } |
| 4626 | if (opt_type == -2 || opt_type == 0) |
| 4627 | type = &t_string; |
| 4628 | else |
| 4629 | type = &t_number; // both number and boolean option |
| 4630 | } |
| 4631 | else if (*var_start == '$') |
| 4632 | { |
| 4633 | dest = dest_env; |
| 4634 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4635 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4636 | } |
| 4637 | else if (*var_start == '@') |
| 4638 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4639 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4640 | { |
| 4641 | emsg_invreg(var_start[1]); |
| 4642 | goto theend; |
| 4643 | } |
| 4644 | dest = dest_reg; |
| 4645 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4646 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4647 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4648 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4649 | { |
| 4650 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4651 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4652 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4653 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4654 | { |
| 4655 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4656 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4657 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4658 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4659 | { |
| 4660 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4661 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4662 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4663 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4664 | { |
| 4665 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4666 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4667 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4668 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4669 | { |
| 4670 | typval_T *vtv; |
| 4671 | int di_flags; |
| 4672 | |
| 4673 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4674 | if (vimvaridx < 0) |
| 4675 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4676 | semsg(_(e_variable_not_found_str), var_start); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4677 | goto theend; |
| 4678 | } |
| 4679 | // We use the current value of "sandbox" here, is that OK? |
| 4680 | if (var_check_ro(di_flags, name, FALSE)) |
| 4681 | goto theend; |
| 4682 | dest = dest_vimvar; |
| 4683 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4684 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4685 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4686 | } |
| 4687 | else |
| 4688 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4689 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4690 | |
| 4691 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4692 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4693 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4694 | semsg(_(e_cannot_use_reserved_name), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4695 | goto theend; |
| 4696 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4697 | |
| 4698 | lvar = lookup_local(var_start, varlen, cctx); |
| 4699 | if (lvar == NULL) |
| 4700 | { |
| 4701 | CLEAR_FIELD(arg_lvar); |
| 4702 | if (lookup_arg(var_start, varlen, |
| 4703 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4704 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4705 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4706 | if (is_decl) |
| 4707 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4708 | semsg(_(e_str_is_used_as_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4709 | goto theend; |
| 4710 | } |
| 4711 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4712 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4713 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4714 | if (lvar != NULL) |
| 4715 | { |
| 4716 | if (is_decl) |
| 4717 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4718 | semsg(_(e_variable_already_declared), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4719 | goto theend; |
| 4720 | } |
| 4721 | else if (lvar->lv_const) |
| 4722 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4723 | semsg(_(e_cannot_assign_to_constant), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4724 | goto theend; |
| 4725 | } |
| 4726 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4727 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4728 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4729 | int script_namespace = varlen > 1 |
| 4730 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4731 | int script_var = (script_namespace |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 4732 | ? lookup_script(var_start + 2, varlen - 2, FALSE) |
| 4733 | : lookup_script(var_start, varlen, TRUE)) == OK; |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4734 | imported_T *import = |
| 4735 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4736 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4737 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4738 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4739 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4740 | |
| 4741 | if (is_decl) |
| 4742 | { |
| 4743 | if (script_namespace) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4744 | semsg(_(e_cannot_declare_script_variable_in_function), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4745 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4746 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4747 | semsg(_(e_variable_already_declared_in_script), |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4748 | name); |
| 4749 | goto theend; |
| 4750 | } |
| 4751 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4752 | == SCRIPT_VERSION_VIM9 |
| 4753 | && script_namespace |
| 4754 | && !script_var && import == NULL) |
| 4755 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4756 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4757 | goto theend; |
| 4758 | } |
| 4759 | |
| 4760 | dest = dest_script; |
| 4761 | |
| 4762 | // existing script-local variables should have a type |
| 4763 | scriptvar_sid = current_sctx.sc_sid; |
| 4764 | if (import != NULL) |
| 4765 | scriptvar_sid = import->imp_sid; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4766 | if (SCRIPT_ID_VALID(scriptvar_sid)) |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4767 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4768 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4769 | rawname, TRUE); |
| 4770 | if (scriptvar_idx > 0) |
| 4771 | { |
| 4772 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4773 | svar_T *sv = |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4774 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4775 | + scriptvar_idx; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4776 | type = sv->sv_type; |
| 4777 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | else if (name[1] == ':' && name[2] != NUL) |
| 4781 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4782 | semsg(_(e_cannot_use_namespaced_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4783 | goto theend; |
| 4784 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4785 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4786 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4787 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4788 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4789 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4790 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4791 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4792 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4793 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4794 | |
| 4795 | if (declare_error) |
| 4796 | { |
| 4797 | vim9_declare_error(name); |
| 4798 | goto theend; |
| 4799 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | // handle "a:name" as a name, not index "name" on "a" |
| 4803 | if (varlen > 1 || var_start[varlen] != ':') |
| 4804 | p = var_end; |
| 4805 | |
| 4806 | if (dest != dest_option) |
| 4807 | { |
| 4808 | if (is_decl && *p == ':') |
| 4809 | { |
| 4810 | // parse optional type: "let var: type = expr" |
| 4811 | if (!VIM_ISWHITE(p[1])) |
| 4812 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4813 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4814 | goto theend; |
| 4815 | } |
| 4816 | p = skipwhite(p + 1); |
| 4817 | type = parse_type(&p, cctx->ctx_type_list); |
| 4818 | has_type = TRUE; |
| 4819 | } |
| 4820 | else if (lvar != NULL) |
| 4821 | type = lvar->lv_type; |
| 4822 | } |
| 4823 | |
| 4824 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4825 | && type->tt_type != VAR_STRING |
| 4826 | && type->tt_type != VAR_ANY) |
| 4827 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4828 | emsg(_(e_can_only_concatenate_to_string)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4829 | goto theend; |
| 4830 | } |
| 4831 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4832 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4833 | { |
| 4834 | if (oplen > 1 && !heredoc) |
| 4835 | { |
| 4836 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4837 | semsg(_(e_cannot_use_operator_on_new_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4838 | goto theend; |
| 4839 | } |
| 4840 | |
| 4841 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4842 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4843 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4844 | goto theend; |
| 4845 | lvar = reserve_local(cctx, var_start, varlen, |
| 4846 | cmdidx == CMD_const, type); |
| 4847 | if (lvar == NULL) |
| 4848 | goto theend; |
| 4849 | new_local = TRUE; |
| 4850 | } |
| 4851 | |
| 4852 | member_type = type; |
| 4853 | if (var_end > var_start + varlen) |
| 4854 | { |
| 4855 | // Something follows after the variable: "var[idx]". |
| 4856 | if (is_decl) |
| 4857 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4858 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4859 | goto theend; |
| 4860 | } |
| 4861 | |
| 4862 | if (var_start[varlen] == '[') |
| 4863 | { |
| 4864 | has_index = TRUE; |
| 4865 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4866 | member_type = &t_any; |
| 4867 | else |
| 4868 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4869 | } |
| 4870 | else |
| 4871 | { |
| 4872 | semsg("Not supported yet: %s", var_start); |
| 4873 | goto theend; |
| 4874 | } |
| 4875 | } |
| 4876 | else if (lvar == &arg_lvar) |
| 4877 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4878 | semsg(_(e_cannot_assign_to_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4879 | goto theend; |
| 4880 | } |
| 4881 | |
| 4882 | if (!heredoc) |
| 4883 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4884 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4885 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4886 | if (oplen > 0 && var_count == 0) |
| 4887 | { |
| 4888 | // skip over the "=" and the expression |
| 4889 | p = skipwhite(op + oplen); |
| 4890 | compile_expr0(&p, cctx); |
| 4891 | } |
| 4892 | } |
| 4893 | else if (oplen > 0) |
| 4894 | { |
| 4895 | type_T *stacktype; |
| 4896 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4897 | // For "var = expr" evaluate the expression. |
| 4898 | if (var_count == 0) |
| 4899 | { |
| 4900 | int r; |
| 4901 | |
| 4902 | // for "+=", "*=", "..=" etc. first load the current value |
| 4903 | if (*op != '=') |
| 4904 | { |
| 4905 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4906 | |
| 4907 | if (has_index) |
| 4908 | { |
| 4909 | // TODO: get member from list or dict |
| 4910 | emsg("Index with operation not supported yet"); |
| 4911 | goto theend; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | // Compile the expression. Temporarily hide the new local |
| 4916 | // variable here, it is not available to this expression. |
| 4917 | if (new_local) |
| 4918 | --cctx->ctx_locals.ga_len; |
| 4919 | instr_count = instr->ga_len; |
| 4920 | p = skipwhite(op + oplen); |
| 4921 | r = compile_expr0(&p, cctx); |
| 4922 | if (new_local) |
| 4923 | ++cctx->ctx_locals.ga_len; |
| 4924 | if (r == FAIL) |
| 4925 | goto theend; |
| 4926 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4927 | else if (semicolon && var_idx == var_count - 1) |
| 4928 | { |
| 4929 | // For "[var; var] = expr" get the rest of the list |
| 4930 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 4931 | goto theend; |
| 4932 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4933 | else |
| 4934 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4935 | // For "[var, var] = expr" get the "var_idx" item from the |
| 4936 | // list. |
| 4937 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 4938 | return FAIL; |
| 4939 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4940 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4941 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 4942 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4943 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4944 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4945 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4946 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4947 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4948 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4949 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4950 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4951 | } |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4952 | else if ((stacktype->tt_type == VAR_FUNC |
| 4953 | || stacktype->tt_type == VAR_PARTIAL) |
| 4954 | && var_wrong_func_name(name, TRUE)) |
| 4955 | { |
| 4956 | goto theend; |
| 4957 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4958 | else |
| 4959 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4960 | // An empty list or dict has a &t_void member, |
| 4961 | // for a variable that implies &t_any. |
| 4962 | if (stacktype == &t_list_empty) |
| 4963 | lvar->lv_type = &t_list_any; |
| 4964 | else if (stacktype == &t_dict_empty) |
| 4965 | lvar->lv_type = &t_dict_any; |
| 4966 | else |
| 4967 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4968 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4969 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 4970 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4971 | { |
| 4972 | type_T *use_type = lvar->lv_type; |
| 4973 | |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 4974 | // without operator type is here, otherwise below |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4975 | if (has_index) |
| 4976 | { |
| 4977 | use_type = use_type->tt_member; |
| 4978 | if (use_type == NULL) |
| 4979 | use_type = &t_void; |
| 4980 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4981 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 4982 | == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4983 | goto theend; |
| 4984 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4985 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4986 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4987 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4988 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4989 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4990 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4991 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4992 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4993 | goto theend; |
| 4994 | } |
| 4995 | else if (!has_type || dest == dest_option) |
| 4996 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4997 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4998 | goto theend; |
| 4999 | } |
| 5000 | else |
| 5001 | { |
| 5002 | // variables are always initialized |
| 5003 | if (ga_grow(instr, 1) == FAIL) |
| 5004 | goto theend; |
| 5005 | switch (member_type->tt_type) |
| 5006 | { |
| 5007 | case VAR_BOOL: |
| 5008 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5009 | break; |
| 5010 | case VAR_FLOAT: |
| 5011 | #ifdef FEAT_FLOAT |
| 5012 | generate_PUSHF(cctx, 0.0); |
| 5013 | #endif |
| 5014 | break; |
| 5015 | case VAR_STRING: |
| 5016 | generate_PUSHS(cctx, NULL); |
| 5017 | break; |
| 5018 | case VAR_BLOB: |
| 5019 | generate_PUSHBLOB(cctx, NULL); |
| 5020 | break; |
| 5021 | case VAR_FUNC: |
| 5022 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5023 | break; |
| 5024 | case VAR_LIST: |
| 5025 | generate_NEWLIST(cctx, 0); |
| 5026 | break; |
| 5027 | case VAR_DICT: |
| 5028 | generate_NEWDICT(cctx, 0); |
| 5029 | break; |
| 5030 | case VAR_JOB: |
| 5031 | generate_PUSHJOB(cctx, NULL); |
| 5032 | break; |
| 5033 | case VAR_CHANNEL: |
| 5034 | generate_PUSHCHANNEL(cctx, NULL); |
| 5035 | break; |
| 5036 | case VAR_NUMBER: |
| 5037 | case VAR_UNKNOWN: |
| 5038 | case VAR_ANY: |
| 5039 | case VAR_PARTIAL: |
| 5040 | case VAR_VOID: |
| 5041 | case VAR_SPECIAL: // cannot happen |
| 5042 | generate_PUSHNR(cctx, 0); |
| 5043 | break; |
| 5044 | } |
| 5045 | } |
| 5046 | if (var_count == 0) |
| 5047 | end = p; |
| 5048 | } |
| 5049 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5050 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5051 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5052 | break; |
| 5053 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5054 | if (oplen > 0 && *op != '=') |
| 5055 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5056 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5057 | type_T *stacktype; |
| 5058 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5059 | if (*op == '.') |
| 5060 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5061 | else |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5062 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5063 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5064 | if ( |
| 5065 | #ifdef FEAT_FLOAT |
| 5066 | // If variable is float operation with number is OK. |
| 5067 | !(expected == &t_float && stacktype == &t_number) && |
| 5068 | #endif |
| 5069 | need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5070 | goto theend; |
| 5071 | |
| 5072 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5073 | { |
| 5074 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 5075 | goto theend; |
| 5076 | } |
| 5077 | else if (*op == '+') |
| 5078 | { |
| 5079 | if (generate_add_instr(cctx, |
| 5080 | operator_type(member_type, stacktype), |
| 5081 | member_type, stacktype) == FAIL) |
| 5082 | goto theend; |
| 5083 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5084 | else if (generate_two_op(cctx, op) == FAIL) |
| 5085 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5086 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5087 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5088 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5089 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5090 | int r; |
| 5091 | |
| 5092 | // Compile the "idx" in "var[idx]". |
| 5093 | if (new_local) |
| 5094 | --cctx->ctx_locals.ga_len; |
| 5095 | p = skipwhite(var_start + varlen + 1); |
| 5096 | r = compile_expr0(&p, cctx); |
| 5097 | if (new_local) |
| 5098 | ++cctx->ctx_locals.ga_len; |
| 5099 | if (r == FAIL) |
| 5100 | goto theend; |
| 5101 | if (*skipwhite(p) != ']') |
| 5102 | { |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 5103 | // this should not happen |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5104 | emsg(_(e_missbrac)); |
| 5105 | goto theend; |
| 5106 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5107 | if (type == &t_any) |
| 5108 | { |
| 5109 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5110 | stack->ga_len - 1]; |
| 5111 | // Index on variable of unknown type: guess the type from the |
| 5112 | // index type: number is dict, otherwise dict. |
| 5113 | // TODO: should do the assignment at runtime |
| 5114 | if (idx_type->tt_type == VAR_NUMBER) |
| 5115 | type = &t_list_any; |
| 5116 | else |
| 5117 | type = &t_dict_any; |
| 5118 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5119 | if (type->tt_type == VAR_DICT |
| 5120 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5121 | goto theend; |
| 5122 | if (type->tt_type == VAR_LIST |
| 5123 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5124 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5125 | { |
| 5126 | emsg(_(e_number_exp)); |
| 5127 | goto theend; |
| 5128 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5129 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5130 | // Load the dict or list. On the stack we then have: |
| 5131 | // - value |
| 5132 | // - index |
| 5133 | // - variable |
| 5134 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5135 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5136 | if (type->tt_type == VAR_LIST) |
| 5137 | { |
| 5138 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5139 | return FAIL; |
| 5140 | } |
| 5141 | else if (type->tt_type == VAR_DICT) |
| 5142 | { |
| 5143 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5144 | return FAIL; |
| 5145 | } |
| 5146 | else |
| 5147 | { |
| 5148 | emsg(_(e_listreq)); |
| 5149 | goto theend; |
| 5150 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5151 | } |
| 5152 | else |
| 5153 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5154 | switch (dest) |
| 5155 | { |
| 5156 | case dest_option: |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 5157 | generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5158 | opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5159 | break; |
| 5160 | case dest_global: |
| 5161 | // include g: with the name, easier to execute that way |
| 5162 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5163 | break; |
| 5164 | case dest_buffer: |
| 5165 | // include b: with the name, easier to execute that way |
| 5166 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5167 | break; |
| 5168 | case dest_window: |
| 5169 | // include w: with the name, easier to execute that way |
| 5170 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5171 | break; |
| 5172 | case dest_tab: |
| 5173 | // include t: with the name, easier to execute that way |
| 5174 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5175 | break; |
| 5176 | case dest_env: |
| 5177 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5178 | break; |
| 5179 | case dest_reg: |
| 5180 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5181 | break; |
| 5182 | case dest_vimvar: |
| 5183 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5184 | break; |
| 5185 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5186 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5187 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5188 | { |
| 5189 | char_u *name_s = name; |
| 5190 | |
| 5191 | // Include s: in the name for store_var() |
| 5192 | if (name[1] != ':') |
| 5193 | { |
| 5194 | int len = (int)STRLEN(name) + 3; |
| 5195 | |
| 5196 | name_s = alloc(len); |
| 5197 | if (name_s == NULL) |
| 5198 | name_s = name; |
| 5199 | else |
| 5200 | vim_snprintf((char *)name_s, len, |
| 5201 | "s:%s", name); |
| 5202 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5203 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5204 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5205 | if (name_s != name) |
| 5206 | vim_free(name_s); |
| 5207 | } |
| 5208 | else |
| 5209 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5210 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5211 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5212 | break; |
| 5213 | case dest_local: |
| 5214 | if (lvar != NULL) |
| 5215 | { |
| 5216 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5217 | + instr->ga_len - 1; |
| 5218 | |
| 5219 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5220 | // ISN_STORE into ISN_STORENR |
| 5221 | if (!lvar->lv_from_outer |
| 5222 | && instr->ga_len == instr_count + 1 |
| 5223 | && isn->isn_type == ISN_PUSHNR) |
| 5224 | { |
| 5225 | varnumber_T val = isn->isn_arg.number; |
| 5226 | |
| 5227 | isn->isn_type = ISN_STORENR; |
| 5228 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5229 | isn->isn_arg.storenr.stnr_val = val; |
| 5230 | if (stack->ga_len > 0) |
| 5231 | --stack->ga_len; |
| 5232 | } |
| 5233 | else if (lvar->lv_from_outer) |
| 5234 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5235 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5236 | else |
| 5237 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5238 | } |
| 5239 | break; |
| 5240 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5241 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5242 | |
| 5243 | if (var_idx + 1 < var_count) |
| 5244 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5245 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5246 | |
| 5247 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5248 | if (var_count > 0 && !semicolon) |
| 5249 | { |
| 5250 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5251 | goto theend; |
| 5252 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5253 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5254 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5255 | |
| 5256 | theend: |
| 5257 | vim_free(name); |
| 5258 | return ret; |
| 5259 | } |
| 5260 | |
| 5261 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5262 | * Check if "name" can be "unlet". |
| 5263 | */ |
| 5264 | int |
| 5265 | check_vim9_unlet(char_u *name) |
| 5266 | { |
| 5267 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5268 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 5269 | // "unlet s:var" is allowed in legacy script. |
| 5270 | if (*name == 's' && !script_is_vim9()) |
| 5271 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5272 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5273 | return FAIL; |
| 5274 | } |
| 5275 | return OK; |
| 5276 | } |
| 5277 | |
| 5278 | /* |
| 5279 | * Callback passed to ex_unletlock(). |
| 5280 | */ |
| 5281 | static int |
| 5282 | compile_unlet( |
| 5283 | lval_T *lvp, |
| 5284 | char_u *name_end, |
| 5285 | exarg_T *eap, |
| 5286 | int deep UNUSED, |
| 5287 | void *coookie) |
| 5288 | { |
| 5289 | cctx_T *cctx = coookie; |
| 5290 | |
| 5291 | if (lvp->ll_tv == NULL) |
| 5292 | { |
| 5293 | char_u *p = lvp->ll_name; |
| 5294 | int cc = *name_end; |
| 5295 | int ret = OK; |
| 5296 | |
| 5297 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5298 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5299 | if (*p == '$') |
| 5300 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5301 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5302 | ret = FAIL; |
| 5303 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5304 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5305 | |
| 5306 | *name_end = cc; |
| 5307 | return ret; |
| 5308 | } |
| 5309 | |
| 5310 | // TODO: unlet {list}[idx] |
| 5311 | // TODO: unlet {dict}[key] |
| 5312 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5313 | return FAIL; |
| 5314 | } |
| 5315 | |
| 5316 | /* |
| 5317 | * compile "unlet var", "lock var" and "unlock var" |
| 5318 | * "arg" points to "var". |
| 5319 | */ |
| 5320 | static char_u * |
| 5321 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5322 | { |
| 5323 | char_u *p = arg; |
| 5324 | |
| 5325 | if (eap->cmdidx != CMD_unlet) |
| 5326 | { |
| 5327 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5328 | return NULL; |
| 5329 | } |
| 5330 | |
| 5331 | if (*p == '!') |
| 5332 | { |
| 5333 | p = skipwhite(p + 1); |
| 5334 | eap->forceit = TRUE; |
| 5335 | } |
| 5336 | |
| 5337 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5338 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5339 | } |
| 5340 | |
| 5341 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5342 | * Compile an :import command. |
| 5343 | */ |
| 5344 | static char_u * |
| 5345 | compile_import(char_u *arg, cctx_T *cctx) |
| 5346 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5347 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | /* |
| 5351 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5352 | */ |
| 5353 | static int |
| 5354 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5355 | { |
| 5356 | garray_T *instr = &cctx->ctx_instr; |
| 5357 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5358 | |
| 5359 | if (endlabel == NULL) |
| 5360 | return FAIL; |
| 5361 | endlabel->el_next = *el; |
| 5362 | *el = endlabel; |
| 5363 | endlabel->el_end_label = instr->ga_len; |
| 5364 | |
| 5365 | generate_JUMP(cctx, when, 0); |
| 5366 | return OK; |
| 5367 | } |
| 5368 | |
| 5369 | static void |
| 5370 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5371 | { |
| 5372 | garray_T *instr = &cctx->ctx_instr; |
| 5373 | |
| 5374 | while (*el != NULL) |
| 5375 | { |
| 5376 | endlabel_T *cur = (*el); |
| 5377 | isn_T *isn; |
| 5378 | |
| 5379 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5380 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5381 | *el = cur->el_next; |
| 5382 | vim_free(cur); |
| 5383 | } |
| 5384 | } |
| 5385 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5386 | static void |
| 5387 | compile_free_jump_to_end(endlabel_T **el) |
| 5388 | { |
| 5389 | while (*el != NULL) |
| 5390 | { |
| 5391 | endlabel_T *cur = (*el); |
| 5392 | |
| 5393 | *el = cur->el_next; |
| 5394 | vim_free(cur); |
| 5395 | } |
| 5396 | } |
| 5397 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5398 | /* |
| 5399 | * Create a new scope and set up the generic items. |
| 5400 | */ |
| 5401 | static scope_T * |
| 5402 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5403 | { |
| 5404 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5405 | |
| 5406 | if (scope == NULL) |
| 5407 | return NULL; |
| 5408 | scope->se_outer = cctx->ctx_scope; |
| 5409 | cctx->ctx_scope = scope; |
| 5410 | scope->se_type = type; |
| 5411 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5412 | return scope; |
| 5413 | } |
| 5414 | |
| 5415 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5416 | * Free the current scope and go back to the outer scope. |
| 5417 | */ |
| 5418 | static void |
| 5419 | drop_scope(cctx_T *cctx) |
| 5420 | { |
| 5421 | scope_T *scope = cctx->ctx_scope; |
| 5422 | |
| 5423 | if (scope == NULL) |
| 5424 | { |
| 5425 | iemsg("calling drop_scope() without a scope"); |
| 5426 | return; |
| 5427 | } |
| 5428 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5429 | switch (scope->se_type) |
| 5430 | { |
| 5431 | case IF_SCOPE: |
| 5432 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5433 | case FOR_SCOPE: |
| 5434 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5435 | case WHILE_SCOPE: |
| 5436 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5437 | case TRY_SCOPE: |
| 5438 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5439 | case NO_SCOPE: |
| 5440 | case BLOCK_SCOPE: |
| 5441 | break; |
| 5442 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5443 | vim_free(scope); |
| 5444 | } |
| 5445 | |
| 5446 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5447 | * compile "if expr" |
| 5448 | * |
| 5449 | * "if expr" Produces instructions: |
| 5450 | * EVAL expr Push result of "expr" |
| 5451 | * JUMP_IF_FALSE end |
| 5452 | * ... body ... |
| 5453 | * end: |
| 5454 | * |
| 5455 | * "if expr | else" Produces instructions: |
| 5456 | * EVAL expr Push result of "expr" |
| 5457 | * JUMP_IF_FALSE else |
| 5458 | * ... body ... |
| 5459 | * JUMP_ALWAYS end |
| 5460 | * else: |
| 5461 | * ... body ... |
| 5462 | * end: |
| 5463 | * |
| 5464 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5465 | * EVAL expr Push result of "expr" |
| 5466 | * JUMP_IF_FALSE elseif |
| 5467 | * ... body ... |
| 5468 | * JUMP_ALWAYS end |
| 5469 | * elseif: |
| 5470 | * EVAL expr Push result of "expr" |
| 5471 | * JUMP_IF_FALSE else |
| 5472 | * ... body ... |
| 5473 | * JUMP_ALWAYS end |
| 5474 | * else: |
| 5475 | * ... body ... |
| 5476 | * end: |
| 5477 | */ |
| 5478 | static char_u * |
| 5479 | compile_if(char_u *arg, cctx_T *cctx) |
| 5480 | { |
| 5481 | char_u *p = arg; |
| 5482 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5483 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5484 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5485 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5486 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5487 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5488 | CLEAR_FIELD(ppconst); |
| 5489 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5490 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5491 | clear_ppconst(&ppconst); |
| 5492 | return NULL; |
| 5493 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5494 | if (cctx->ctx_skip == SKIP_YES) |
| 5495 | clear_ppconst(&ppconst); |
| 5496 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5497 | { |
| 5498 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5499 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5500 | clear_ppconst(&ppconst); |
| 5501 | } |
| 5502 | else |
| 5503 | { |
| 5504 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5505 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5506 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5507 | return NULL; |
| 5508 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5509 | |
| 5510 | scope = new_scope(cctx, IF_SCOPE); |
| 5511 | if (scope == NULL) |
| 5512 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5513 | scope->se_skip_save = skip_save; |
| 5514 | // "is_had_return" will be reset if any block does not end in :return |
| 5515 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5516 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5517 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5518 | { |
| 5519 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5520 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5521 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5522 | } |
| 5523 | else |
| 5524 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5525 | |
| 5526 | return p; |
| 5527 | } |
| 5528 | |
| 5529 | static char_u * |
| 5530 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5531 | { |
| 5532 | char_u *p = arg; |
| 5533 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5534 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5535 | isn_T *isn; |
| 5536 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5537 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5538 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5539 | |
| 5540 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5541 | { |
| 5542 | emsg(_(e_elseif_without_if)); |
| 5543 | return NULL; |
| 5544 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5545 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5546 | if (!cctx->ctx_had_return) |
| 5547 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5548 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5549 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5550 | { |
| 5551 | 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] | 5552 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5553 | return NULL; |
| 5554 | // previous "if" or "elseif" jumps here |
| 5555 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5556 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5557 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5558 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5559 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5560 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5561 | if (cctx->ctx_skip == SKIP_YES) |
| 5562 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5563 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5564 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5565 | clear_ppconst(&ppconst); |
| 5566 | return NULL; |
| 5567 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5568 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5569 | if (scope->se_skip_save == SKIP_YES) |
| 5570 | clear_ppconst(&ppconst); |
| 5571 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5572 | { |
| 5573 | // The expression results in a constant. |
| 5574 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5575 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5576 | clear_ppconst(&ppconst); |
| 5577 | scope->se_u.se_if.is_if_label = -1; |
| 5578 | } |
| 5579 | else |
| 5580 | { |
| 5581 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5582 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5583 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5584 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5585 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5586 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5587 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5588 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5589 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5590 | |
| 5591 | return p; |
| 5592 | } |
| 5593 | |
| 5594 | static char_u * |
| 5595 | compile_else(char_u *arg, cctx_T *cctx) |
| 5596 | { |
| 5597 | char_u *p = arg; |
| 5598 | garray_T *instr = &cctx->ctx_instr; |
| 5599 | isn_T *isn; |
| 5600 | scope_T *scope = cctx->ctx_scope; |
| 5601 | |
| 5602 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5603 | { |
| 5604 | emsg(_(e_else_without_if)); |
| 5605 | return NULL; |
| 5606 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5607 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5608 | if (!cctx->ctx_had_return) |
| 5609 | scope->se_u.se_if.is_had_return = FALSE; |
| 5610 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5611 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5612 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5613 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5614 | // 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] | 5615 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5616 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5617 | if (!cctx->ctx_had_return |
| 5618 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5619 | JUMP_ALWAYS, cctx) == FAIL) |
| 5620 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5621 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5622 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5623 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5624 | { |
| 5625 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5626 | { |
| 5627 | // previous "if" or "elseif" jumps here |
| 5628 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5629 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5630 | scope->se_u.se_if.is_if_label = -1; |
| 5631 | } |
| 5632 | } |
| 5633 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5634 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5635 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5636 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5637 | |
| 5638 | return p; |
| 5639 | } |
| 5640 | |
| 5641 | static char_u * |
| 5642 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5643 | { |
| 5644 | scope_T *scope = cctx->ctx_scope; |
| 5645 | ifscope_T *ifscope; |
| 5646 | garray_T *instr = &cctx->ctx_instr; |
| 5647 | isn_T *isn; |
| 5648 | |
| 5649 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5650 | { |
| 5651 | emsg(_(e_endif_without_if)); |
| 5652 | return NULL; |
| 5653 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5654 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5655 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5656 | if (!cctx->ctx_had_return) |
| 5657 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5658 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5659 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5660 | { |
| 5661 | // previous "if" or "elseif" jumps here |
| 5662 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5663 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5664 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5665 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5666 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5667 | cctx->ctx_skip = scope->se_skip_save; |
| 5668 | |
| 5669 | // If all the blocks end in :return and there is an :else then the |
| 5670 | // had_return flag is set. |
| 5671 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5672 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5673 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5674 | return arg; |
| 5675 | } |
| 5676 | |
| 5677 | /* |
| 5678 | * compile "for var in expr" |
| 5679 | * |
| 5680 | * Produces instructions: |
| 5681 | * PUSHNR -1 |
| 5682 | * STORE loop-idx Set index to -1 |
| 5683 | * EVAL expr Push result of "expr" |
| 5684 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5685 | * - if beyond end, jump to "end" |
| 5686 | * - otherwise get item from list and push it |
| 5687 | * STORE var Store item in "var" |
| 5688 | * ... body ... |
| 5689 | * JUMP top Jump back to repeat |
| 5690 | * end: DROP Drop the result of "expr" |
| 5691 | * |
| 5692 | */ |
| 5693 | static char_u * |
| 5694 | compile_for(char_u *arg, cctx_T *cctx) |
| 5695 | { |
| 5696 | char_u *p; |
| 5697 | size_t varlen; |
| 5698 | garray_T *instr = &cctx->ctx_instr; |
| 5699 | garray_T *stack = &cctx->ctx_type_stack; |
| 5700 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5701 | lvar_T *loop_lvar; // loop iteration variable |
| 5702 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5703 | type_T *vartype; |
| 5704 | |
| 5705 | // TODO: list of variables: "for [key, value] in dict" |
| 5706 | // parse "var" |
| 5707 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5708 | ; |
| 5709 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5710 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5711 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5712 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5713 | semsg(_(e_variable_already_declared), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5714 | return NULL; |
| 5715 | } |
| 5716 | |
| 5717 | // consume "in" |
| 5718 | p = skipwhite(p); |
| 5719 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5720 | { |
| 5721 | emsg(_(e_missing_in)); |
| 5722 | return NULL; |
| 5723 | } |
| 5724 | p = skipwhite(p + 2); |
| 5725 | |
| 5726 | |
| 5727 | scope = new_scope(cctx, FOR_SCOPE); |
| 5728 | if (scope == NULL) |
| 5729 | return NULL; |
| 5730 | |
| 5731 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5732 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5733 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5734 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5735 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5736 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5737 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5738 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5739 | |
| 5740 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5741 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5742 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5743 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5744 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5745 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5746 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5747 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5748 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5749 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5750 | |
| 5751 | // compile "expr", it remains on the stack until "endfor" |
| 5752 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5753 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5754 | { |
| 5755 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5756 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5757 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5758 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5759 | // Now that we know the type of "var", check that it is a list, now or at |
| 5760 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5761 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5762 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5763 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5764 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5765 | return NULL; |
| 5766 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5767 | 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] | 5768 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5769 | |
| 5770 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5771 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5772 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5773 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5774 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5775 | |
| 5776 | return arg; |
| 5777 | } |
| 5778 | |
| 5779 | /* |
| 5780 | * compile "endfor" |
| 5781 | */ |
| 5782 | static char_u * |
| 5783 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5784 | { |
| 5785 | garray_T *instr = &cctx->ctx_instr; |
| 5786 | scope_T *scope = cctx->ctx_scope; |
| 5787 | forscope_T *forscope; |
| 5788 | isn_T *isn; |
| 5789 | |
| 5790 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5791 | { |
| 5792 | emsg(_(e_for)); |
| 5793 | return NULL; |
| 5794 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5795 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5796 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5797 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5798 | |
| 5799 | // At end of ":for" scope jump back to the FOR instruction. |
| 5800 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5801 | |
| 5802 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5803 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5804 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5805 | |
| 5806 | // Fill in the "end" label any BREAK statements |
| 5807 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5808 | |
| 5809 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5810 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5811 | return NULL; |
| 5812 | |
| 5813 | vim_free(scope); |
| 5814 | |
| 5815 | return arg; |
| 5816 | } |
| 5817 | |
| 5818 | /* |
| 5819 | * compile "while expr" |
| 5820 | * |
| 5821 | * Produces instructions: |
| 5822 | * top: EVAL expr Push result of "expr" |
| 5823 | * JUMP_IF_FALSE end jump if false |
| 5824 | * ... body ... |
| 5825 | * JUMP top Jump back to repeat |
| 5826 | * end: |
| 5827 | * |
| 5828 | */ |
| 5829 | static char_u * |
| 5830 | compile_while(char_u *arg, cctx_T *cctx) |
| 5831 | { |
| 5832 | char_u *p = arg; |
| 5833 | garray_T *instr = &cctx->ctx_instr; |
| 5834 | scope_T *scope; |
| 5835 | |
| 5836 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5837 | if (scope == NULL) |
| 5838 | return NULL; |
| 5839 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5840 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5841 | |
| 5842 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5843 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5844 | return NULL; |
| 5845 | |
| 5846 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5847 | 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] | 5848 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5849 | return FAIL; |
| 5850 | |
| 5851 | return p; |
| 5852 | } |
| 5853 | |
| 5854 | /* |
| 5855 | * compile "endwhile" |
| 5856 | */ |
| 5857 | static char_u * |
| 5858 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5859 | { |
| 5860 | scope_T *scope = cctx->ctx_scope; |
| 5861 | |
| 5862 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5863 | { |
| 5864 | emsg(_(e_while)); |
| 5865 | return NULL; |
| 5866 | } |
| 5867 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5868 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5869 | |
| 5870 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5871 | 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] | 5872 | |
| 5873 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5874 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5875 | 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] | 5876 | |
| 5877 | vim_free(scope); |
| 5878 | |
| 5879 | return arg; |
| 5880 | } |
| 5881 | |
| 5882 | /* |
| 5883 | * compile "continue" |
| 5884 | */ |
| 5885 | static char_u * |
| 5886 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5887 | { |
| 5888 | scope_T *scope = cctx->ctx_scope; |
| 5889 | |
| 5890 | for (;;) |
| 5891 | { |
| 5892 | if (scope == NULL) |
| 5893 | { |
| 5894 | emsg(_(e_continue)); |
| 5895 | return NULL; |
| 5896 | } |
| 5897 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5898 | break; |
| 5899 | scope = scope->se_outer; |
| 5900 | } |
| 5901 | |
| 5902 | // Jump back to the FOR or WHILE instruction. |
| 5903 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5904 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5905 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5906 | return arg; |
| 5907 | } |
| 5908 | |
| 5909 | /* |
| 5910 | * compile "break" |
| 5911 | */ |
| 5912 | static char_u * |
| 5913 | compile_break(char_u *arg, cctx_T *cctx) |
| 5914 | { |
| 5915 | scope_T *scope = cctx->ctx_scope; |
| 5916 | endlabel_T **el; |
| 5917 | |
| 5918 | for (;;) |
| 5919 | { |
| 5920 | if (scope == NULL) |
| 5921 | { |
| 5922 | emsg(_(e_break)); |
| 5923 | return NULL; |
| 5924 | } |
| 5925 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5926 | break; |
| 5927 | scope = scope->se_outer; |
| 5928 | } |
| 5929 | |
| 5930 | // Jump to the end of the FOR or WHILE loop. |
| 5931 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5932 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5933 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5934 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5935 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5936 | return FAIL; |
| 5937 | |
| 5938 | return arg; |
| 5939 | } |
| 5940 | |
| 5941 | /* |
| 5942 | * compile "{" start of block |
| 5943 | */ |
| 5944 | static char_u * |
| 5945 | compile_block(char_u *arg, cctx_T *cctx) |
| 5946 | { |
| 5947 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5948 | return NULL; |
| 5949 | return skipwhite(arg + 1); |
| 5950 | } |
| 5951 | |
| 5952 | /* |
| 5953 | * compile end of block: drop one scope |
| 5954 | */ |
| 5955 | static void |
| 5956 | compile_endblock(cctx_T *cctx) |
| 5957 | { |
| 5958 | scope_T *scope = cctx->ctx_scope; |
| 5959 | |
| 5960 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5961 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5962 | vim_free(scope); |
| 5963 | } |
| 5964 | |
| 5965 | /* |
| 5966 | * compile "try" |
| 5967 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5968 | * finally. |
| 5969 | * Creates another scope for the "try" block itself. |
| 5970 | * TRY instruction sets up exception handling at runtime. |
| 5971 | * |
| 5972 | * "try" |
| 5973 | * TRY -> catch1, -> finally push trystack entry |
| 5974 | * ... try block |
| 5975 | * "throw {exception}" |
| 5976 | * EVAL {exception} |
| 5977 | * THROW create exception |
| 5978 | * ... try block |
| 5979 | * " catch {expr}" |
| 5980 | * JUMP -> finally |
| 5981 | * catch1: PUSH exeception |
| 5982 | * EVAL {expr} |
| 5983 | * MATCH |
| 5984 | * JUMP nomatch -> catch2 |
| 5985 | * CATCH remove exception |
| 5986 | * ... catch block |
| 5987 | * " catch" |
| 5988 | * JUMP -> finally |
| 5989 | * catch2: CATCH remove exception |
| 5990 | * ... catch block |
| 5991 | * " finally" |
| 5992 | * finally: |
| 5993 | * ... finally block |
| 5994 | * " endtry" |
| 5995 | * ENDTRY pop trystack entry, may rethrow |
| 5996 | */ |
| 5997 | static char_u * |
| 5998 | compile_try(char_u *arg, cctx_T *cctx) |
| 5999 | { |
| 6000 | garray_T *instr = &cctx->ctx_instr; |
| 6001 | scope_T *try_scope; |
| 6002 | scope_T *scope; |
| 6003 | |
| 6004 | // scope that holds the jumps that go to catch/finally/endtry |
| 6005 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6006 | if (try_scope == NULL) |
| 6007 | return NULL; |
| 6008 | |
| 6009 | // "catch" is set when the first ":catch" is found. |
| 6010 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6011 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6012 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6013 | return NULL; |
| 6014 | |
| 6015 | // scope for the try block itself |
| 6016 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6017 | if (scope == NULL) |
| 6018 | return NULL; |
| 6019 | |
| 6020 | return arg; |
| 6021 | } |
| 6022 | |
| 6023 | /* |
| 6024 | * compile "catch {expr}" |
| 6025 | */ |
| 6026 | static char_u * |
| 6027 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6028 | { |
| 6029 | scope_T *scope = cctx->ctx_scope; |
| 6030 | garray_T *instr = &cctx->ctx_instr; |
| 6031 | char_u *p; |
| 6032 | isn_T *isn; |
| 6033 | |
| 6034 | // end block scope from :try or :catch |
| 6035 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6036 | compile_endblock(cctx); |
| 6037 | scope = cctx->ctx_scope; |
| 6038 | |
| 6039 | // Error if not in a :try scope |
| 6040 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6041 | { |
| 6042 | emsg(_(e_catch)); |
| 6043 | return NULL; |
| 6044 | } |
| 6045 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6046 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6047 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6048 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6049 | return NULL; |
| 6050 | } |
| 6051 | |
| 6052 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6053 | 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] | 6054 | JUMP_ALWAYS, cctx) == FAIL) |
| 6055 | return NULL; |
| 6056 | |
| 6057 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6058 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6059 | if (isn->isn_arg.try.try_catch == 0) |
| 6060 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6061 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6062 | { |
| 6063 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6064 | 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] | 6065 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6066 | } |
| 6067 | |
| 6068 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6069 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6070 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6071 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6072 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6073 | } |
| 6074 | else |
| 6075 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6076 | char_u *end; |
| 6077 | char_u *pat; |
| 6078 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6079 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6080 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6081 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6082 | // Push v:exception, push {expr} and MATCH |
| 6083 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6084 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6085 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6086 | if (*end != *p) |
| 6087 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 6088 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6089 | vim_free(tofree); |
| 6090 | return FAIL; |
| 6091 | } |
| 6092 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6093 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6094 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6095 | len = (int)(end - tofree); |
| 6096 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6097 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6098 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6099 | if (pat == NULL) |
| 6100 | return FAIL; |
| 6101 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6102 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6103 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6104 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6105 | return NULL; |
| 6106 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6107 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6108 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6109 | return NULL; |
| 6110 | } |
| 6111 | |
| 6112 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6113 | return NULL; |
| 6114 | |
| 6115 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6116 | return NULL; |
| 6117 | return p; |
| 6118 | } |
| 6119 | |
| 6120 | static char_u * |
| 6121 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6122 | { |
| 6123 | scope_T *scope = cctx->ctx_scope; |
| 6124 | garray_T *instr = &cctx->ctx_instr; |
| 6125 | isn_T *isn; |
| 6126 | |
| 6127 | // end block scope from :try or :catch |
| 6128 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6129 | compile_endblock(cctx); |
| 6130 | scope = cctx->ctx_scope; |
| 6131 | |
| 6132 | // Error if not in a :try scope |
| 6133 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6134 | { |
| 6135 | emsg(_(e_finally)); |
| 6136 | return NULL; |
| 6137 | } |
| 6138 | |
| 6139 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6140 | 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] | 6141 | if (isn->isn_arg.try.try_finally != 0) |
| 6142 | { |
| 6143 | emsg(_(e_finally_dup)); |
| 6144 | return NULL; |
| 6145 | } |
| 6146 | |
| 6147 | // 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] | 6148 | 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] | 6149 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6150 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6151 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6152 | { |
| 6153 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6154 | 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] | 6155 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6156 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6157 | } |
| 6158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6159 | // TODO: set index in ts_finally_label jumps |
| 6160 | |
| 6161 | return arg; |
| 6162 | } |
| 6163 | |
| 6164 | static char_u * |
| 6165 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6166 | { |
| 6167 | scope_T *scope = cctx->ctx_scope; |
| 6168 | garray_T *instr = &cctx->ctx_instr; |
| 6169 | isn_T *isn; |
| 6170 | |
| 6171 | // end block scope from :catch or :finally |
| 6172 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6173 | compile_endblock(cctx); |
| 6174 | scope = cctx->ctx_scope; |
| 6175 | |
| 6176 | // Error if not in a :try scope |
| 6177 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6178 | { |
| 6179 | if (scope == NULL) |
| 6180 | emsg(_(e_no_endtry)); |
| 6181 | else if (scope->se_type == WHILE_SCOPE) |
| 6182 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6183 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6184 | emsg(_(e_endfor)); |
| 6185 | else |
| 6186 | emsg(_(e_endif)); |
| 6187 | return NULL; |
| 6188 | } |
| 6189 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6190 | 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] | 6191 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6192 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6193 | emsg(_(e_missing_catch_or_finally)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6194 | return NULL; |
| 6195 | } |
| 6196 | |
| 6197 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6198 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6199 | 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] | 6200 | |
| 6201 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6202 | if (isn->isn_arg.try.try_catch == 0) |
| 6203 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6204 | if (isn->isn_arg.try.try_finally == 0) |
| 6205 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6206 | |
| 6207 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6208 | { |
| 6209 | // Last catch without match jumps here |
| 6210 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6211 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6212 | } |
| 6213 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6214 | compile_endblock(cctx); |
| 6215 | |
| 6216 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6217 | return NULL; |
| 6218 | return arg; |
| 6219 | } |
| 6220 | |
| 6221 | /* |
| 6222 | * compile "throw {expr}" |
| 6223 | */ |
| 6224 | static char_u * |
| 6225 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6226 | { |
| 6227 | char_u *p = skipwhite(arg); |
| 6228 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6229 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6230 | return NULL; |
| 6231 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6232 | return NULL; |
| 6233 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6234 | return NULL; |
| 6235 | |
| 6236 | return p; |
| 6237 | } |
| 6238 | |
| 6239 | /* |
| 6240 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6241 | * compile "echomsg expr" |
| 6242 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6243 | * compile "execute expr" |
| 6244 | */ |
| 6245 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6246 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6247 | { |
| 6248 | char_u *p = arg; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6249 | char_u *prev; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6250 | int count = 0; |
| 6251 | |
| 6252 | for (;;) |
| 6253 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6254 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6255 | return NULL; |
| 6256 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6257 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6258 | p = skipwhite(p); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6259 | if (ends_excmd2(prev, p)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6260 | break; |
| 6261 | } |
| 6262 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6263 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6264 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6265 | else if (cmdidx == CMD_execute) |
| 6266 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6267 | else if (cmdidx == CMD_echomsg) |
| 6268 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6269 | else |
| 6270 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6271 | return p; |
| 6272 | } |
| 6273 | |
| 6274 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6275 | * A command that is not compiled, execute with legacy code. |
| 6276 | */ |
| 6277 | static char_u * |
| 6278 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6279 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6280 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6281 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6282 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6283 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6284 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6285 | goto theend; |
| 6286 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6287 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6288 | { |
| 6289 | long argt = excmd_get_argt(eap->cmdidx); |
| 6290 | int usefilter = FALSE; |
| 6291 | |
| 6292 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6293 | |
| 6294 | // If the command can be followed by a bar, find the bar and truncate |
| 6295 | // it, so that the following command can be compiled. |
| 6296 | // The '|' is overwritten with a NUL, it is put back below. |
| 6297 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6298 | && *eap->arg == '!') |
| 6299 | // :w !filter or :r !filter or :r! filter |
| 6300 | usefilter = TRUE; |
| 6301 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6302 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 6303 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6304 | separate_nextcmd(eap); |
| 6305 | if (eap->nextcmd != NULL) |
| 6306 | nextcmd = eap->nextcmd; |
| 6307 | } |
| 6308 | } |
| 6309 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6310 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6311 | { |
| 6312 | // expand filename in "syntax include [@group] filename" |
| 6313 | has_expr = TRUE; |
| 6314 | eap->arg = skipwhite(eap->arg + 7); |
| 6315 | if (*eap->arg == '@') |
| 6316 | eap->arg = skiptowhite(eap->arg); |
| 6317 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6318 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6319 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6320 | { |
| 6321 | int count = 0; |
| 6322 | char_u *start = skipwhite(line); |
| 6323 | |
| 6324 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6325 | // PUSHS ":cmd xxx" |
| 6326 | // eval expr1 |
| 6327 | // PUSHS "yyy" |
| 6328 | // eval expr2 |
| 6329 | // PUSHS "zzz" |
| 6330 | // EXECCONCAT 5 |
| 6331 | for (;;) |
| 6332 | { |
| 6333 | if (p > start) |
| 6334 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6335 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6336 | ++count; |
| 6337 | } |
| 6338 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6339 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6340 | return NULL; |
| 6341 | may_generate_2STRING(-1, cctx); |
| 6342 | ++count; |
| 6343 | p = skipwhite(p); |
| 6344 | if (*p != '`') |
| 6345 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6346 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6347 | return NULL; |
| 6348 | } |
| 6349 | start = p + 1; |
| 6350 | |
| 6351 | p = (char_u *)strstr((char *)start, "`="); |
| 6352 | if (p == NULL) |
| 6353 | { |
| 6354 | if (*skipwhite(start) != NUL) |
| 6355 | { |
| 6356 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6357 | ++count; |
| 6358 | } |
| 6359 | break; |
| 6360 | } |
| 6361 | } |
| 6362 | generate_EXECCONCAT(cctx, count); |
| 6363 | } |
| 6364 | else |
| 6365 | generate_EXEC(cctx, line); |
| 6366 | |
| 6367 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6368 | if (*nextcmd != NUL) |
| 6369 | { |
| 6370 | // the parser expects a pointer to the bar, put it back |
| 6371 | --nextcmd; |
| 6372 | *nextcmd = '|'; |
| 6373 | } |
| 6374 | |
| 6375 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6379 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6380 | * 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] | 6381 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6382 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6383 | add_def_function(ufunc_T *ufunc) |
| 6384 | { |
| 6385 | dfunc_T *dfunc; |
| 6386 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6387 | if (def_functions.ga_len == 0) |
| 6388 | { |
| 6389 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6390 | // wasn't set. |
| 6391 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6392 | return FAIL; |
| 6393 | ++def_functions.ga_len; |
| 6394 | } |
| 6395 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6396 | // Add the function to "def_functions". |
| 6397 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6398 | return FAIL; |
| 6399 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6400 | CLEAR_POINTER(dfunc); |
| 6401 | dfunc->df_idx = def_functions.ga_len; |
| 6402 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6403 | dfunc->df_ufunc = ufunc; |
| 6404 | ++def_functions.ga_len; |
| 6405 | return OK; |
| 6406 | } |
| 6407 | |
| 6408 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6409 | * After ex_function() has collected all the function lines: parse and compile |
| 6410 | * the lines into instructions. |
| 6411 | * Adds the function to "def_functions". |
| 6412 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6413 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6414 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6415 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6416 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6417 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6418 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6419 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6420 | 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] | 6421 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6422 | char_u *line = NULL; |
| 6423 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6424 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6425 | cctx_T cctx; |
| 6426 | garray_T *instr; |
| 6427 | int called_emsg_before = called_emsg; |
| 6428 | int ret = FAIL; |
| 6429 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6430 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6431 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6432 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6433 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6434 | // When using a function that was compiled before: Free old instructions. |
| 6435 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6436 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6437 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6438 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6439 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6440 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6441 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6442 | else |
| 6443 | { |
| 6444 | if (add_def_function(ufunc) == FAIL) |
| 6445 | return FAIL; |
| 6446 | new_def_function = TRUE; |
| 6447 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6448 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6449 | ufunc->uf_def_status = UF_COMPILING; |
| 6450 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6451 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6452 | cctx.ctx_ufunc = ufunc; |
| 6453 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6454 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6455 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6456 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6457 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6458 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6459 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6460 | instr = &cctx.ctx_instr; |
| 6461 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6462 | // Set the context to the function, it may be compiled when called from |
| 6463 | // another script. Set the script version to the most modern one. |
| 6464 | // The line number will be set in next_line_from_context(). |
| 6465 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6466 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6467 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6468 | // Make sure error messages are OK. |
| 6469 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6470 | if (do_estack_push) |
| 6471 | estack_push_ufunc(ufunc, 1); |
| 6472 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6473 | if (ufunc->uf_def_args.ga_len > 0) |
| 6474 | { |
| 6475 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6476 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6477 | int i; |
| 6478 | char_u *arg; |
| 6479 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6480 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6481 | |
| 6482 | // Produce instructions for the default values of optional arguments. |
| 6483 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6484 | // where to start when the function is called, depending on the number |
| 6485 | // of arguments. |
| 6486 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6487 | if (ufunc->uf_def_arg_idx == NULL) |
| 6488 | goto erret; |
| 6489 | for (i = 0; i < count; ++i) |
| 6490 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6491 | garray_T *stack = &cctx.ctx_type_stack; |
| 6492 | type_T *val_type; |
| 6493 | int arg_idx = first_def_arg + i; |
| 6494 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6495 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6496 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6497 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6498 | goto erret; |
| 6499 | |
| 6500 | // If no type specified use the type of the default value. |
| 6501 | // Otherwise check that the default value type matches the |
| 6502 | // specified type. |
| 6503 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6504 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6505 | { |
| 6506 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6507 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6508 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 6509 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 6510 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6511 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6512 | |
| 6513 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6514 | goto erret; |
| 6515 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6516 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6517 | |
| 6518 | if (did_set_arg_type) |
| 6519 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | /* |
| 6523 | * Loop over all the lines of the function and generate instructions. |
| 6524 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6525 | for (;;) |
| 6526 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6527 | exarg_T ea; |
| 6528 | cmdmod_T save_cmdmod; |
| 6529 | int starts_with_colon = FALSE; |
| 6530 | char_u *cmd; |
| 6531 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6532 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6533 | // Bail out on the first error to avoid a flood of errors and report |
| 6534 | // the right line number when inside try/catch. |
| 6535 | if (emsg_before != called_emsg) |
| 6536 | goto erret; |
| 6537 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6538 | if (line != NULL && *line == '|') |
| 6539 | // the line continues after a '|' |
| 6540 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6541 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6542 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6543 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6544 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6545 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6546 | goto erret; |
| 6547 | } |
| 6548 | else |
| 6549 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6550 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6551 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6552 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6553 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6554 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6555 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6556 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6557 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6558 | ea.cmdlinep = &line; |
| 6559 | ea.cmd = skipwhite(line); |
| 6560 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6561 | // Some things can be recognized by the first character. |
| 6562 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6563 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6564 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6565 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6566 | if (ea.cmd[1] != '{') |
| 6567 | { |
| 6568 | line = (char_u *)""; |
| 6569 | continue; |
| 6570 | } |
| 6571 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6572 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6573 | case '}': |
| 6574 | { |
| 6575 | // "}" ends a block scope |
| 6576 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6577 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6578 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6579 | if (stype == BLOCK_SCOPE) |
| 6580 | { |
| 6581 | compile_endblock(&cctx); |
| 6582 | line = ea.cmd; |
| 6583 | } |
| 6584 | else |
| 6585 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6586 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6587 | goto erret; |
| 6588 | } |
| 6589 | if (line != NULL) |
| 6590 | line = skipwhite(ea.cmd + 1); |
| 6591 | continue; |
| 6592 | } |
| 6593 | |
| 6594 | case '{': |
| 6595 | // "{" starts a block scope |
| 6596 | // "{'a': 1}->func() is something else |
| 6597 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6598 | { |
| 6599 | line = compile_block(ea.cmd, &cctx); |
| 6600 | continue; |
| 6601 | } |
| 6602 | break; |
| 6603 | |
| 6604 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6605 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6606 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | /* |
| 6610 | * COMMAND MODIFIERS |
| 6611 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6612 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6613 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6614 | { |
| 6615 | if (errormsg != NULL) |
| 6616 | goto erret; |
| 6617 | // empty line or comment |
| 6618 | line = (char_u *)""; |
| 6619 | continue; |
| 6620 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6621 | // TODO: use modifiers in the command |
| 6622 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6623 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6624 | |
| 6625 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6626 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6627 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6628 | { |
| 6629 | if (*ea.cmd == '(') |
| 6630 | // not for "call()" |
| 6631 | ea.cmd = p; |
| 6632 | else |
| 6633 | ea.cmd = skipwhite(ea.cmd); |
| 6634 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6635 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6636 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6637 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6638 | char_u *pskip; |
| 6639 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6640 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6641 | // find what follows. |
| 6642 | // Skip over "var.member", "var[idx]" and the like. |
| 6643 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6644 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6645 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6646 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6647 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6648 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6649 | char_u *var_end; |
| 6650 | int oplen; |
| 6651 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6652 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6653 | if (ea.cmd[0] == '@') |
| 6654 | var_end = ea.cmd + 2; |
| 6655 | else |
| 6656 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6657 | FNE_CHECK_START | FNE_INCL_BR); |
| 6658 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6659 | if (oplen > 0) |
| 6660 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6661 | size_t len = p - ea.cmd; |
| 6662 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6663 | // Recognize an assignment if we recognize the variable |
| 6664 | // name: |
| 6665 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6666 | // "local = expr" where "local" is a local var. |
| 6667 | // "script = expr" where "script" is a script-local var. |
| 6668 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6669 | // "&opt = expr" |
| 6670 | // "$ENV = expr" |
| 6671 | // "@r = expr" |
| 6672 | if (*ea.cmd == '&' |
| 6673 | || *ea.cmd == '$' |
| 6674 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6675 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6676 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6677 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6678 | NULL, &cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 6679 | || lookup_script(ea.cmd, len, FALSE) == OK |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6680 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6681 | { |
| 6682 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6683 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6684 | goto erret; |
| 6685 | continue; |
| 6686 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6687 | } |
| 6688 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6689 | |
| 6690 | if (*ea.cmd == '[') |
| 6691 | { |
| 6692 | // [var, var] = expr |
| 6693 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6694 | if (line == NULL) |
| 6695 | goto erret; |
| 6696 | if (line != ea.cmd) |
| 6697 | continue; |
| 6698 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | /* |
| 6702 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6703 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6704 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6705 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 6706 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6707 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6708 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6709 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6710 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6711 | if (!starts_with_colon) |
| 6712 | { |
| 6713 | emsg(_(e_colon_required_before_a_range)); |
| 6714 | goto erret; |
| 6715 | } |
| 6716 | if (ends_excmd2(line, ea.cmd)) |
| 6717 | { |
| 6718 | // A range without a command: jump to the line. |
| 6719 | // TODO: compile to a more efficient command, possibly |
| 6720 | // calling parse_cmd_address(). |
| 6721 | ea.cmdidx = CMD_SIZE; |
| 6722 | line = compile_exec(line, &ea, &cctx); |
| 6723 | goto nextline; |
| 6724 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6725 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6726 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6727 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6728 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6729 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6730 | |
| 6731 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6732 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6733 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6734 | { |
| 6735 | line += STRLEN(line); |
| 6736 | continue; |
| 6737 | } |
| 6738 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6739 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6740 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6741 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6742 | // CMD_let cannot happen, compile_assignment() above is used |
| 6743 | iemsg("Command from find_ex_command() not handled"); |
| 6744 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6745 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6746 | } |
| 6747 | |
| 6748 | p = skipwhite(p); |
| 6749 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6750 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6751 | && ea.cmdidx != CMD_elseif |
| 6752 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6753 | && ea.cmdidx != CMD_endif |
| 6754 | && ea.cmdidx != CMD_endfor |
| 6755 | && ea.cmdidx != CMD_endwhile |
| 6756 | && ea.cmdidx != CMD_catch |
| 6757 | && ea.cmdidx != CMD_finally |
| 6758 | && ea.cmdidx != CMD_endtry) |
| 6759 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6760 | emsg(_(e_unreachable_code_after_return)); |
| 6761 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6762 | } |
| 6763 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6764 | switch (ea.cmdidx) |
| 6765 | { |
| 6766 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6767 | ea.arg = p; |
| 6768 | line = compile_nested_function(&ea, &cctx); |
| 6769 | break; |
| 6770 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6771 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6772 | // TODO: should we allow this, e.g. to declare a global |
| 6773 | // function? |
| 6774 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6775 | goto erret; |
| 6776 | |
| 6777 | case CMD_return: |
| 6778 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6779 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6780 | break; |
| 6781 | |
| 6782 | case CMD_let: |
| 6783 | case CMD_const: |
| 6784 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6785 | if (line == p) |
| 6786 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6787 | break; |
| 6788 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6789 | case CMD_unlet: |
| 6790 | case CMD_unlockvar: |
| 6791 | case CMD_lockvar: |
| 6792 | line = compile_unletlock(p, &ea, &cctx); |
| 6793 | break; |
| 6794 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6795 | case CMD_import: |
| 6796 | line = compile_import(p, &cctx); |
| 6797 | break; |
| 6798 | |
| 6799 | case CMD_if: |
| 6800 | line = compile_if(p, &cctx); |
| 6801 | break; |
| 6802 | case CMD_elseif: |
| 6803 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6804 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6805 | break; |
| 6806 | case CMD_else: |
| 6807 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6808 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6809 | break; |
| 6810 | case CMD_endif: |
| 6811 | line = compile_endif(p, &cctx); |
| 6812 | break; |
| 6813 | |
| 6814 | case CMD_while: |
| 6815 | line = compile_while(p, &cctx); |
| 6816 | break; |
| 6817 | case CMD_endwhile: |
| 6818 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6819 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6820 | break; |
| 6821 | |
| 6822 | case CMD_for: |
| 6823 | line = compile_for(p, &cctx); |
| 6824 | break; |
| 6825 | case CMD_endfor: |
| 6826 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6827 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6828 | break; |
| 6829 | case CMD_continue: |
| 6830 | line = compile_continue(p, &cctx); |
| 6831 | break; |
| 6832 | case CMD_break: |
| 6833 | line = compile_break(p, &cctx); |
| 6834 | break; |
| 6835 | |
| 6836 | case CMD_try: |
| 6837 | line = compile_try(p, &cctx); |
| 6838 | break; |
| 6839 | case CMD_catch: |
| 6840 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6841 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6842 | break; |
| 6843 | case CMD_finally: |
| 6844 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6845 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6846 | break; |
| 6847 | case CMD_endtry: |
| 6848 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6849 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6850 | break; |
| 6851 | case CMD_throw: |
| 6852 | line = compile_throw(p, &cctx); |
| 6853 | break; |
| 6854 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6855 | case CMD_eval: |
| 6856 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6857 | goto erret; |
| 6858 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6859 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6860 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6861 | |
| 6862 | line = skipwhite(p); |
| 6863 | break; |
| 6864 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6865 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6866 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6867 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6868 | case CMD_echomsg: |
| 6869 | case CMD_echoerr: |
| 6870 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6871 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6872 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6873 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6874 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6875 | case CMD_append: |
| 6876 | case CMD_change: |
| 6877 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 6878 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6879 | case CMD_xit: |
| 6880 | not_in_vim9(&ea); |
| 6881 | goto erret; |
| 6882 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6883 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6884 | if (cctx.ctx_skip != SKIP_YES) |
| 6885 | { |
| 6886 | semsg(_(e_invalid_command_str), ea.cmd); |
| 6887 | goto erret; |
| 6888 | } |
| 6889 | // We don't check for a next command here. |
| 6890 | line = (char_u *)""; |
| 6891 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6892 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6893 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 6894 | if (cctx.ctx_skip == SKIP_YES) |
| 6895 | { |
| 6896 | // We don't check for a next command here. |
| 6897 | line = (char_u *)""; |
| 6898 | } |
| 6899 | else |
| 6900 | { |
| 6901 | // Not recognized, execute with do_cmdline_cmd(). |
| 6902 | ea.arg = p; |
| 6903 | line = compile_exec(line, &ea, &cctx); |
| 6904 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6905 | break; |
| 6906 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6907 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6908 | if (line == NULL) |
| 6909 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6910 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6911 | |
| 6912 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6913 | { |
| 6914 | iemsg("Type stack underflow"); |
| 6915 | goto erret; |
| 6916 | } |
| 6917 | } |
| 6918 | |
| 6919 | if (cctx.ctx_scope != NULL) |
| 6920 | { |
| 6921 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6922 | emsg(_(e_endif)); |
| 6923 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6924 | emsg(_(e_endwhile)); |
| 6925 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6926 | emsg(_(e_endfor)); |
| 6927 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6928 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6929 | goto erret; |
| 6930 | } |
| 6931 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6932 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6933 | { |
| 6934 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6935 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6936 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6937 | goto erret; |
| 6938 | } |
| 6939 | |
| 6940 | // Return zero if there is no return at the end. |
| 6941 | generate_PUSHNR(&cctx, 0); |
| 6942 | generate_instr(&cctx, ISN_RETURN); |
| 6943 | } |
| 6944 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6945 | { |
| 6946 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6947 | + ufunc->uf_dfunc_idx; |
| 6948 | dfunc->df_deleted = FALSE; |
| 6949 | dfunc->df_instr = instr->ga_data; |
| 6950 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6951 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6952 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6953 | if (cctx.ctx_outer_used) |
| 6954 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6955 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6956 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6957 | |
| 6958 | ret = OK; |
| 6959 | |
| 6960 | erret: |
| 6961 | if (ret == FAIL) |
| 6962 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6963 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6964 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6965 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6966 | |
| 6967 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6968 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6969 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6970 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6971 | // If using the last entry in the table and it was added above, we |
| 6972 | // might as well remove it. |
| 6973 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 6974 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6975 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6976 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6977 | ufunc->uf_dfunc_idx = 0; |
| 6978 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6979 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6980 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6981 | while (cctx.ctx_scope != NULL) |
| 6982 | drop_scope(&cctx); |
| 6983 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6984 | // Don't execute this function body. |
| 6985 | ga_clear_strings(&ufunc->uf_lines); |
| 6986 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6987 | if (errormsg != NULL) |
| 6988 | emsg(errormsg); |
| 6989 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6990 | emsg(_(e_compile_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6994 | if (do_estack_push) |
| 6995 | estack_pop(); |
| 6996 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6997 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6998 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6999 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7000 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7001 | } |
| 7002 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7003 | void |
| 7004 | set_function_type(ufunc_T *ufunc) |
| 7005 | { |
| 7006 | int varargs = ufunc->uf_va_name != NULL; |
| 7007 | int argcount = ufunc->uf_args.ga_len; |
| 7008 | |
| 7009 | // Create a type for the function, with the return type and any |
| 7010 | // argument types. |
| 7011 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7012 | // The type is included in "tt_args". |
| 7013 | if (argcount > 0 || varargs) |
| 7014 | { |
| 7015 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7016 | argcount, &ufunc->uf_type_list); |
| 7017 | // Add argument types to the function type. |
| 7018 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7019 | argcount + varargs, |
| 7020 | &ufunc->uf_type_list) == FAIL) |
| 7021 | return; |
| 7022 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7023 | ufunc->uf_func_type->tt_min_argcount = |
| 7024 | argcount - ufunc->uf_def_args.ga_len; |
| 7025 | if (ufunc->uf_arg_types == NULL) |
| 7026 | { |
| 7027 | int i; |
| 7028 | |
| 7029 | // lambda does not have argument types. |
| 7030 | for (i = 0; i < argcount; ++i) |
| 7031 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7032 | } |
| 7033 | else |
| 7034 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7035 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7036 | if (varargs) |
| 7037 | { |
| 7038 | ufunc->uf_func_type->tt_args[argcount] = |
| 7039 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7040 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7041 | } |
| 7042 | } |
| 7043 | else |
| 7044 | // No arguments, can use a predefined type. |
| 7045 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7046 | argcount, &ufunc->uf_type_list); |
| 7047 | } |
| 7048 | |
| 7049 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7050 | /* |
| 7051 | * Delete an instruction, free what it contains. |
| 7052 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7053 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7054 | delete_instr(isn_T *isn) |
| 7055 | { |
| 7056 | switch (isn->isn_type) |
| 7057 | { |
| 7058 | case ISN_EXEC: |
| 7059 | case ISN_LOADENV: |
| 7060 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7061 | case ISN_LOADB: |
| 7062 | case ISN_LOADW: |
| 7063 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7064 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7065 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7066 | case ISN_PUSHEXC: |
| 7067 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7068 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7069 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7070 | case ISN_STOREB: |
| 7071 | case ISN_STOREW: |
| 7072 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7073 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7074 | vim_free(isn->isn_arg.string); |
| 7075 | break; |
| 7076 | |
| 7077 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7078 | case ISN_STORES: |
| 7079 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7080 | break; |
| 7081 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7082 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7083 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7084 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7085 | break; |
| 7086 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7087 | case ISN_STOREOPT: |
| 7088 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7089 | break; |
| 7090 | |
| 7091 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7092 | blob_unref(isn->isn_arg.blob); |
| 7093 | break; |
| 7094 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7095 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7096 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7097 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7098 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7099 | break; |
| 7100 | |
| 7101 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7102 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7103 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7104 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7105 | break; |
| 7106 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7107 | case ISN_UCALL: |
| 7108 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7109 | break; |
| 7110 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7111 | case ISN_FUNCREF: |
| 7112 | { |
| 7113 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7114 | + isn->isn_arg.funcref.fr_func; |
| 7115 | func_ptr_unref(dfunc->df_ufunc); |
| 7116 | } |
| 7117 | break; |
| 7118 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7119 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7120 | { |
| 7121 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7122 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7123 | |
| 7124 | if (ufunc != NULL) |
| 7125 | { |
| 7126 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7127 | clear_def_function(ufunc); |
| 7128 | ufunc->uf_dfunc_idx = 0; |
| 7129 | func_ptr_unref(ufunc); |
| 7130 | } |
| 7131 | |
| 7132 | vim_free(lambda); |
| 7133 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7134 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7135 | break; |
| 7136 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7137 | case ISN_2BOOL: |
| 7138 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 7139 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7140 | case ISN_ADDBLOB: |
| 7141 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7142 | case ISN_ANYINDEX: |
| 7143 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7144 | case ISN_BCALL: |
| 7145 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7146 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7147 | case ISN_CHECKNR: |
| 7148 | case ISN_CHECKTYPE: |
| 7149 | case ISN_COMPAREANY: |
| 7150 | case ISN_COMPAREBLOB: |
| 7151 | case ISN_COMPAREBOOL: |
| 7152 | case ISN_COMPAREDICT: |
| 7153 | case ISN_COMPAREFLOAT: |
| 7154 | case ISN_COMPAREFUNC: |
| 7155 | case ISN_COMPARELIST: |
| 7156 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7157 | case ISN_COMPARESPECIAL: |
| 7158 | case ISN_COMPARESTRING: |
| 7159 | case ISN_CONCAT: |
| 7160 | case ISN_DCALL: |
| 7161 | case ISN_DROP: |
| 7162 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7163 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7164 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7165 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7166 | case ISN_EXECCONCAT: |
| 7167 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7168 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7169 | case ISN_GETITEM: |
| 7170 | case ISN_JUMP: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7171 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 7172 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7173 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7174 | case ISN_LOADBDICT: |
| 7175 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7176 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7177 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7178 | case ISN_LOADSCRIPT: |
| 7179 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7180 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7181 | case ISN_LOADWDICT: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7182 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7183 | case ISN_NEGATENR: |
| 7184 | case ISN_NEWDICT: |
| 7185 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7186 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7187 | case ISN_OPFLOAT: |
| 7188 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7189 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7190 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7191 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7192 | case ISN_PUSHF: |
| 7193 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7194 | case ISN_PUSHSPEC: |
| 7195 | case ISN_RETURN: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7196 | case ISN_SHUFFLE: |
| 7197 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7198 | case ISN_STORE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7199 | case ISN_STOREDICT: |
| 7200 | case ISN_STORELIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7201 | case ISN_STORENR: |
| 7202 | case ISN_STOREOUTER: |
| 7203 | case ISN_STOREREG: |
| 7204 | case ISN_STORESCRIPT: |
| 7205 | case ISN_STOREV: |
| 7206 | case ISN_STRINDEX: |
| 7207 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7208 | case ISN_THROW: |
| 7209 | case ISN_TRY: |
| 7210 | // nothing allocated |
| 7211 | break; |
| 7212 | } |
| 7213 | } |
| 7214 | |
| 7215 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7216 | * Free all instructions for "dfunc". |
| 7217 | */ |
| 7218 | static void |
| 7219 | delete_def_function_contents(dfunc_T *dfunc) |
| 7220 | { |
| 7221 | int idx; |
| 7222 | |
| 7223 | ga_clear(&dfunc->df_def_args_isn); |
| 7224 | |
| 7225 | if (dfunc->df_instr != NULL) |
| 7226 | { |
| 7227 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7228 | delete_instr(dfunc->df_instr + idx); |
| 7229 | VIM_CLEAR(dfunc->df_instr); |
| 7230 | } |
| 7231 | |
| 7232 | dfunc->df_deleted = TRUE; |
| 7233 | } |
| 7234 | |
| 7235 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7236 | * When a user function is deleted, clear the contents of any associated def |
| 7237 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7238 | */ |
| 7239 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7240 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7241 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7242 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7243 | { |
| 7244 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7245 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7246 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7247 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7248 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7249 | } |
| 7250 | } |
| 7251 | |
| 7252 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7253 | /* |
| 7254 | * Free all functions defined with ":def". |
| 7255 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7256 | void |
| 7257 | free_def_functions(void) |
| 7258 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7259 | int idx; |
| 7260 | |
| 7261 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7262 | { |
| 7263 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7264 | |
| 7265 | delete_def_function_contents(dfunc); |
| 7266 | } |
| 7267 | |
| 7268 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7269 | } |
| 7270 | #endif |
| 7271 | |
| 7272 | |
| 7273 | #endif // FEAT_EVAL |