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 | |
| 26 | /* |
| 27 | * Chain of jump instructions where the end label needs to be set. |
| 28 | */ |
| 29 | typedef struct endlabel_S endlabel_T; |
| 30 | struct endlabel_S { |
| 31 | endlabel_T *el_next; // chain end_label locations |
| 32 | int el_end_label; // instruction idx where to set end |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * info specific for the scope of :if / elseif / else |
| 37 | */ |
| 38 | typedef struct { |
| 39 | int is_if_label; // instruction idx at IF or ELSEIF |
| 40 | endlabel_T *is_end_label; // instructions to set end label |
| 41 | } ifscope_T; |
| 42 | |
| 43 | /* |
| 44 | * info specific for the scope of :while |
| 45 | */ |
| 46 | typedef struct { |
| 47 | int ws_top_label; // instruction idx at WHILE |
| 48 | endlabel_T *ws_end_label; // instructions to set end |
| 49 | } whilescope_T; |
| 50 | |
| 51 | /* |
| 52 | * info specific for the scope of :for |
| 53 | */ |
| 54 | typedef struct { |
| 55 | int fs_top_label; // instruction idx at FOR |
| 56 | endlabel_T *fs_end_label; // break instructions |
| 57 | } forscope_T; |
| 58 | |
| 59 | /* |
| 60 | * info specific for the scope of :try |
| 61 | */ |
| 62 | typedef struct { |
| 63 | int ts_try_label; // instruction idx at TRY |
| 64 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 65 | int ts_catch_label; // instruction idx of last CATCH |
| 66 | int ts_caught_all; // "catch" without argument encountered |
| 67 | } tryscope_T; |
| 68 | |
| 69 | typedef enum { |
| 70 | NO_SCOPE, |
| 71 | IF_SCOPE, |
| 72 | WHILE_SCOPE, |
| 73 | FOR_SCOPE, |
| 74 | TRY_SCOPE, |
| 75 | BLOCK_SCOPE |
| 76 | } scopetype_T; |
| 77 | |
| 78 | /* |
| 79 | * Info for one scope, pointed to by "ctx_scope". |
| 80 | */ |
| 81 | typedef struct scope_S scope_T; |
| 82 | struct scope_S { |
| 83 | scope_T *se_outer; // scope containing this one |
| 84 | scopetype_T se_type; |
| 85 | int se_local_count; // ctx_locals.ga_len before scope |
| 86 | union { |
| 87 | ifscope_T se_if; |
| 88 | whilescope_T se_while; |
| 89 | forscope_T se_for; |
| 90 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 91 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 96 | */ |
| 97 | typedef struct { |
| 98 | char_u *lv_name; |
| 99 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 100 | int lv_idx; // index of the variable on the stack |
| 101 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 102 | int lv_const; // when TRUE cannot be assigned to |
| 103 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 104 | } lvar_T; |
| 105 | |
| 106 | /* |
| 107 | * Context for compiling lines of Vim script. |
| 108 | * Stores info about the local variables and condition stack. |
| 109 | */ |
| 110 | struct cctx_S { |
| 111 | ufunc_T *ctx_ufunc; // current function |
| 112 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 113 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | garray_T ctx_instr; // generated instructions |
| 115 | |
| 116 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 117 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 118 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 119 | int ctx_closure_count; // number of closures created in the |
| 120 | // function |
| 121 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 122 | garray_T ctx_imports; // imported items |
| 123 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 124 | int ctx_skip; // when TRUE skip commands, when FALSE skip |
| 125 | // commands after "else" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 126 | scope_T *ctx_scope; // current scope, NULL at toplevel |
| 127 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 128 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 129 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 130 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 133 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 137 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
| 138 | |
| 139 | static int compile_expr1(char_u **arg, cctx_T *cctx); |
| 140 | static int compile_expr2(char_u **arg, cctx_T *cctx); |
| 141 | static int compile_expr3(char_u **arg, cctx_T *cctx); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 142 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 143 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
| 144 | static int check_type(type_T *expected, type_T *actual, int give_msg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 145 | |
| 146 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 147 | * Lookup variable "name" in the local scope and return it. |
| 148 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 149 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 150 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 152 | { |
| 153 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 154 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 155 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 156 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 157 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 158 | |
| 159 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 161 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 162 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 163 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 164 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 165 | { |
| 166 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 167 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 168 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | |
| 171 | // Find local in outer function scope. |
| 172 | if (cctx->ctx_outer != NULL) |
| 173 | { |
| 174 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 175 | if (lvar != NULL) |
| 176 | { |
| 177 | // TODO: are there situations we should not mark the outer scope as |
| 178 | // used? |
| 179 | cctx->ctx_outer_used = TRUE; |
| 180 | lvar->lv_from_outer = TRUE; |
| 181 | return lvar; |
| 182 | } |
| 183 | } |
| 184 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 185 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 189 | * Lookup an argument in the current function and an enclosing function. |
| 190 | * Returns the argument index in "idxp" |
| 191 | * Returns the argument type in "type" |
| 192 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 193 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 194 | */ |
| 195 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 196 | lookup_arg( |
| 197 | char_u *name, |
| 198 | size_t len, |
| 199 | int *idxp, |
| 200 | type_T **type, |
| 201 | int *gen_load_outer, |
| 202 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | { |
| 204 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 206 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 207 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 208 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 209 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 210 | { |
| 211 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 212 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 213 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 214 | { |
| 215 | if (idxp != NULL) |
| 216 | { |
| 217 | // Arguments are located above the frame pointer. One further |
| 218 | // if there is a vararg argument |
| 219 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 220 | + STACK_FRAME_SIZE) |
| 221 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 222 | |
| 223 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 224 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 225 | else |
| 226 | *type = &t_any; |
| 227 | } |
| 228 | return OK; |
| 229 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 230 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 231 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 232 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 233 | if (va_name != NULL |
| 234 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 235 | { |
| 236 | if (idxp != NULL) |
| 237 | { |
| 238 | // varargs is always the last argument |
| 239 | *idxp = -STACK_FRAME_SIZE - 1; |
| 240 | *type = cctx->ctx_ufunc->uf_va_type; |
| 241 | } |
| 242 | return OK; |
| 243 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 244 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 245 | if (cctx->ctx_outer != NULL) |
| 246 | { |
| 247 | // Lookup the name for an argument of the outer function. |
| 248 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 249 | == OK) |
| 250 | { |
| 251 | *gen_load_outer = TRUE; |
| 252 | return OK; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Lookup a variable in the current script. |
| 261 | * Returns OK or FAIL. |
| 262 | */ |
| 263 | static int |
| 264 | lookup_script(char_u *name, size_t len) |
| 265 | { |
| 266 | int cc; |
| 267 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 268 | dictitem_T *di; |
| 269 | |
| 270 | cc = name[len]; |
| 271 | name[len] = NUL; |
| 272 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 273 | name[len] = cc; |
| 274 | return di == NULL ? FAIL: OK; |
| 275 | } |
| 276 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 279 | * compilation context "cctx". |
| 280 | * Return FAIL and give an error if it defined. |
| 281 | */ |
| 282 | int |
| 283 | check_defined(char_u *p, int len, cctx_T *cctx) |
| 284 | { |
| 285 | if (lookup_script(p, len) == OK |
| 286 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 287 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 288 | || find_imported(p, len, cctx) != NULL))) |
| 289 | { |
| 290 | semsg("E1073: imported name already defined: %s", p); |
| 291 | return FAIL; |
| 292 | } |
| 293 | return OK; |
| 294 | } |
| 295 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 296 | /* |
| 297 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 298 | * be freed later. |
| 299 | */ |
| 300 | static type_T * |
| 301 | alloc_type(garray_T *type_gap) |
| 302 | { |
| 303 | type_T *type; |
| 304 | |
| 305 | if (ga_grow(type_gap, 1) == FAIL) |
| 306 | return NULL; |
| 307 | type = ALLOC_CLEAR_ONE(type_T); |
| 308 | if (type != NULL) |
| 309 | { |
| 310 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 311 | ++type_gap->ga_len; |
| 312 | } |
| 313 | return type; |
| 314 | } |
| 315 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 316 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 317 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 318 | { |
| 319 | type_T *type; |
| 320 | |
| 321 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 322 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 323 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 324 | if (member_type->tt_type == VAR_VOID |
| 325 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 326 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 327 | if (member_type->tt_type == VAR_BOOL) |
| 328 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 329 | if (member_type->tt_type == VAR_NUMBER) |
| 330 | return &t_list_number; |
| 331 | if (member_type->tt_type == VAR_STRING) |
| 332 | return &t_list_string; |
| 333 | |
| 334 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 335 | type = alloc_type(type_gap); |
| 336 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 337 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 338 | type->tt_type = VAR_LIST; |
| 339 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 340 | type->tt_argcount = 0; |
| 341 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 342 | return type; |
| 343 | } |
| 344 | |
| 345 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 346 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 347 | { |
| 348 | type_T *type; |
| 349 | |
| 350 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 351 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 352 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 353 | if (member_type->tt_type == VAR_VOID |
| 354 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 355 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 356 | if (member_type->tt_type == VAR_BOOL) |
| 357 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 358 | if (member_type->tt_type == VAR_NUMBER) |
| 359 | return &t_dict_number; |
| 360 | if (member_type->tt_type == VAR_STRING) |
| 361 | return &t_dict_string; |
| 362 | |
| 363 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 364 | type = alloc_type(type_gap); |
| 365 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 366 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 367 | type->tt_type = VAR_DICT; |
| 368 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 369 | type->tt_argcount = 0; |
| 370 | type->tt_args = NULL; |
| 371 | return type; |
| 372 | } |
| 373 | |
| 374 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 375 | * Allocate a new type for a function. |
| 376 | */ |
| 377 | static type_T * |
| 378 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 379 | { |
| 380 | type_T *type = alloc_type(type_gap); |
| 381 | |
| 382 | if (type == NULL) |
| 383 | return &t_any; |
| 384 | type->tt_type = VAR_FUNC; |
| 385 | type->tt_member = ret_type; |
| 386 | type->tt_argcount = argcount; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 392 | * Get a function type, based on the return type "ret_type". |
| 393 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 394 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 395 | */ |
| 396 | static type_T * |
| 397 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 398 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 399 | // recognize commonly used types |
| 400 | if (argcount <= 0) |
| 401 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 402 | if (ret_type == &t_unknown) |
| 403 | { |
| 404 | // (argcount == 0) is not possible |
| 405 | return &t_func_unknown; |
| 406 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 407 | if (ret_type == &t_void) |
| 408 | { |
| 409 | if (argcount == 0) |
| 410 | return &t_func_0_void; |
| 411 | else |
| 412 | return &t_func_void; |
| 413 | } |
| 414 | if (ret_type == &t_any) |
| 415 | { |
| 416 | if (argcount == 0) |
| 417 | return &t_func_0_any; |
| 418 | else |
| 419 | return &t_func_any; |
| 420 | } |
| 421 | if (ret_type == &t_number) |
| 422 | { |
| 423 | if (argcount == 0) |
| 424 | return &t_func_0_number; |
| 425 | else |
| 426 | return &t_func_number; |
| 427 | } |
| 428 | if (ret_type == &t_string) |
| 429 | { |
| 430 | if (argcount == 0) |
| 431 | return &t_func_0_string; |
| 432 | else |
| 433 | return &t_func_string; |
| 434 | } |
| 435 | } |
| 436 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 437 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 440 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 441 | * For a function type, reserve space for "argcount" argument types (including |
| 442 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 443 | */ |
| 444 | static int |
| 445 | func_type_add_arg_types( |
| 446 | type_T *functype, |
| 447 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 448 | garray_T *type_gap) |
| 449 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 450 | // To make it easy to free the space needed for the argument types, add the |
| 451 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 452 | if (ga_grow(type_gap, 1) == FAIL) |
| 453 | return FAIL; |
| 454 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 455 | if (functype->tt_args == NULL) |
| 456 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 457 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 458 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 459 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | return OK; |
| 461 | } |
| 462 | |
| 463 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 464 | * Return the type_T for a typval. Only for primitive types. |
| 465 | */ |
| 466 | static type_T * |
| 467 | typval2type(typval_T *tv) |
| 468 | { |
| 469 | if (tv->v_type == VAR_NUMBER) |
| 470 | return &t_number; |
| 471 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 472 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 473 | if (tv->v_type == VAR_STRING) |
| 474 | return &t_string; |
| 475 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 476 | return &t_list_string; |
| 477 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 478 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 479 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 482 | static void |
| 483 | type_mismatch(type_T *expected, type_T *actual) |
| 484 | { |
| 485 | char *tofree1, *tofree2; |
| 486 | |
| 487 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 488 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 489 | vim_free(tofree1); |
| 490 | vim_free(tofree2); |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 495 | { |
| 496 | char *tofree1, *tofree2; |
| 497 | |
| 498 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 499 | argidx, |
| 500 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 501 | vim_free(tofree1); |
| 502 | vim_free(tofree2); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Check if the expected and actual types match. |
| 507 | * Does not allow for assigning "any" to a specific type. |
| 508 | */ |
| 509 | static int |
| 510 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 511 | { |
| 512 | int ret = OK; |
| 513 | |
| 514 | // When expected is "unknown" we accept any actual type. |
| 515 | // When expected is "any" we accept any actual type except "void". |
| 516 | if (expected->tt_type != VAR_UNKNOWN |
| 517 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 518 | |
| 519 | { |
| 520 | if (expected->tt_type != actual->tt_type) |
| 521 | { |
| 522 | if (give_msg) |
| 523 | type_mismatch(expected, actual); |
| 524 | return FAIL; |
| 525 | } |
| 526 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 527 | { |
| 528 | // "unknown" is used for an empty list or dict |
| 529 | if (actual->tt_member != &t_unknown) |
| 530 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 531 | } |
| 532 | else if (expected->tt_type == VAR_FUNC) |
| 533 | { |
| 534 | if (expected->tt_member != &t_unknown) |
| 535 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 536 | if (ret == OK && expected->tt_argcount != -1 |
| 537 | && (actual->tt_argcount < expected->tt_min_argcount |
| 538 | || actual->tt_argcount > expected->tt_argcount)) |
| 539 | ret = FAIL; |
| 540 | } |
| 541 | if (ret == FAIL && give_msg) |
| 542 | type_mismatch(expected, actual); |
| 543 | } |
| 544 | return ret; |
| 545 | } |
| 546 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 547 | ///////////////////////////////////////////////////////////////////// |
| 548 | // Following generate_ functions expect the caller to call ga_grow(). |
| 549 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 550 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL |
| 551 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK |
| 552 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 553 | /* |
| 554 | * Generate an instruction without arguments. |
| 555 | * Returns a pointer to the new instruction, NULL if failed. |
| 556 | */ |
| 557 | static isn_T * |
| 558 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 559 | { |
| 560 | garray_T *instr = &cctx->ctx_instr; |
| 561 | isn_T *isn; |
| 562 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 563 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 564 | if (ga_grow(instr, 1) == FAIL) |
| 565 | return NULL; |
| 566 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 567 | isn->isn_type = isn_type; |
| 568 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 569 | ++instr->ga_len; |
| 570 | |
| 571 | return isn; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Generate an instruction without arguments. |
| 576 | * "drop" will be removed from the stack. |
| 577 | * Returns a pointer to the new instruction, NULL if failed. |
| 578 | */ |
| 579 | static isn_T * |
| 580 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 581 | { |
| 582 | garray_T *stack = &cctx->ctx_type_stack; |
| 583 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 584 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 585 | stack->ga_len -= drop; |
| 586 | return generate_instr(cctx, isn_type); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 591 | */ |
| 592 | static isn_T * |
| 593 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 594 | { |
| 595 | isn_T *isn; |
| 596 | garray_T *stack = &cctx->ctx_type_stack; |
| 597 | |
| 598 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 599 | return NULL; |
| 600 | |
| 601 | if (ga_grow(stack, 1) == FAIL) |
| 602 | return NULL; |
| 603 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 604 | ++stack->ga_len; |
| 605 | |
| 606 | return isn; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 611 | */ |
| 612 | static int |
| 613 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 614 | { |
| 615 | isn_T *isn; |
| 616 | garray_T *stack = &cctx->ctx_type_stack; |
| 617 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 618 | |
| 619 | if ((*type)->tt_type == VAR_STRING) |
| 620 | return OK; |
| 621 | *type = &t_string; |
| 622 | |
| 623 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 624 | return FAIL; |
| 625 | isn->isn_arg.number = offset; |
| 626 | |
| 627 | return OK; |
| 628 | } |
| 629 | |
| 630 | static int |
| 631 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 632 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 633 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 634 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 635 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 636 | { |
| 637 | if (*op == '+') |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 638 | emsg(_("E1035: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 639 | else |
| 640 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 641 | return FAIL; |
| 642 | } |
| 643 | return OK; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Generate an instruction with two arguments. The instruction depends on the |
| 648 | * type of the arguments. |
| 649 | */ |
| 650 | static int |
| 651 | generate_two_op(cctx_T *cctx, char_u *op) |
| 652 | { |
| 653 | garray_T *stack = &cctx->ctx_type_stack; |
| 654 | type_T *type1; |
| 655 | type_T *type2; |
| 656 | vartype_T vartype; |
| 657 | isn_T *isn; |
| 658 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 659 | RETURN_OK_IF_SKIP(cctx); |
| 660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 661 | // Get the known type of the two items on the stack. If they are matching |
| 662 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 663 | // checking. |
| 664 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 665 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 666 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 667 | if (type1->tt_type == type2->tt_type |
| 668 | && (type1->tt_type == VAR_NUMBER |
| 669 | || type1->tt_type == VAR_LIST |
| 670 | #ifdef FEAT_FLOAT |
| 671 | || type1->tt_type == VAR_FLOAT |
| 672 | #endif |
| 673 | || type1->tt_type == VAR_BLOB)) |
| 674 | vartype = type1->tt_type; |
| 675 | |
| 676 | switch (*op) |
| 677 | { |
| 678 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 679 | && type1->tt_type != VAR_ANY |
| 680 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 681 | && check_number_or_float( |
| 682 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 683 | return FAIL; |
| 684 | isn = generate_instr_drop(cctx, |
| 685 | vartype == VAR_NUMBER ? ISN_OPNR |
| 686 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 687 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 688 | #ifdef FEAT_FLOAT |
| 689 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 690 | #endif |
| 691 | : ISN_OPANY, 1); |
| 692 | if (isn != NULL) |
| 693 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 694 | break; |
| 695 | |
| 696 | case '-': |
| 697 | case '*': |
| 698 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 699 | op) == FAIL) |
| 700 | return FAIL; |
| 701 | if (vartype == VAR_NUMBER) |
| 702 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 703 | #ifdef FEAT_FLOAT |
| 704 | else if (vartype == VAR_FLOAT) |
| 705 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 706 | #endif |
| 707 | else |
| 708 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 709 | if (isn != NULL) |
| 710 | isn->isn_arg.op.op_type = *op == '*' |
| 711 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 712 | break; |
| 713 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 714 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 715 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 716 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 717 | && type2->tt_type != VAR_NUMBER)) |
| 718 | { |
| 719 | emsg(_("E1035: % requires number arguments")); |
| 720 | return FAIL; |
| 721 | } |
| 722 | isn = generate_instr_drop(cctx, |
| 723 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 724 | if (isn != NULL) |
| 725 | isn->isn_arg.op.op_type = EXPR_REM; |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 730 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 731 | { |
| 732 | type_T *type = &t_any; |
| 733 | |
| 734 | #ifdef FEAT_FLOAT |
| 735 | // float+number and number+float results in float |
| 736 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 737 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 738 | type = &t_float; |
| 739 | #endif |
| 740 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 741 | } |
| 742 | |
| 743 | return OK; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 748 | */ |
| 749 | static int |
| 750 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 751 | { |
| 752 | isntype_T isntype = ISN_DROP; |
| 753 | isn_T *isn; |
| 754 | garray_T *stack = &cctx->ctx_type_stack; |
| 755 | vartype_T type1; |
| 756 | vartype_T type2; |
| 757 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 758 | RETURN_OK_IF_SKIP(cctx); |
| 759 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 760 | // Get the known type of the two items on the stack. If they are matching |
| 761 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 762 | // checking. |
| 763 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 764 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 765 | if (type1 == VAR_UNKNOWN) |
| 766 | type1 = VAR_ANY; |
| 767 | if (type2 == VAR_UNKNOWN) |
| 768 | type2 = VAR_ANY; |
| 769 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 770 | if (type1 == type2) |
| 771 | { |
| 772 | switch (type1) |
| 773 | { |
| 774 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 775 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 776 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 777 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 778 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 779 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 780 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 781 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 782 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 783 | default: isntype = ISN_COMPAREANY; break; |
| 784 | } |
| 785 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 786 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 787 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 788 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 789 | isntype = ISN_COMPAREANY; |
| 790 | |
| 791 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 792 | && (isntype == ISN_COMPAREBOOL |
| 793 | || isntype == ISN_COMPARESPECIAL |
| 794 | || isntype == ISN_COMPARENR |
| 795 | || isntype == ISN_COMPAREFLOAT)) |
| 796 | { |
| 797 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 798 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
| 799 | return FAIL; |
| 800 | } |
| 801 | if (isntype == ISN_DROP |
| 802 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 803 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 804 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 805 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 806 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 807 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 808 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 809 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 810 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 811 | vartype_name(type1), vartype_name(type2)); |
| 812 | return FAIL; |
| 813 | } |
| 814 | |
| 815 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 816 | return FAIL; |
| 817 | isn->isn_arg.op.op_type = exptype; |
| 818 | isn->isn_arg.op.op_ic = ic; |
| 819 | |
| 820 | // takes two arguments, puts one bool back |
| 821 | if (stack->ga_len >= 2) |
| 822 | { |
| 823 | --stack->ga_len; |
| 824 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 825 | } |
| 826 | |
| 827 | return OK; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Generate an ISN_2BOOL instruction. |
| 832 | */ |
| 833 | static int |
| 834 | generate_2BOOL(cctx_T *cctx, int invert) |
| 835 | { |
| 836 | isn_T *isn; |
| 837 | garray_T *stack = &cctx->ctx_type_stack; |
| 838 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 839 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 840 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 841 | return FAIL; |
| 842 | isn->isn_arg.number = invert; |
| 843 | |
| 844 | // type becomes bool |
| 845 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 846 | |
| 847 | return OK; |
| 848 | } |
| 849 | |
| 850 | static int |
| 851 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 852 | { |
| 853 | isn_T *isn; |
| 854 | garray_T *stack = &cctx->ctx_type_stack; |
| 855 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 856 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 857 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 858 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 859 | // TODO: whole type, e.g. for a function also arg and return types |
| 860 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 861 | isn->isn_arg.type.ct_off = offset; |
| 862 | |
| 863 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 864 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 865 | |
| 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 870 | * Check that |
| 871 | * - "actual" is "expected" type or |
| 872 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 873 | * - return FAIL. |
| 874 | */ |
| 875 | static int |
| 876 | need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx) |
| 877 | { |
| 878 | if (check_type(expected, actual, FALSE) == OK) |
| 879 | return OK; |
| 880 | if (actual->tt_type != VAR_ANY |
| 881 | && actual->tt_type != VAR_UNKNOWN |
| 882 | && !(actual->tt_type == VAR_FUNC |
| 883 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 884 | { |
| 885 | type_mismatch(expected, actual); |
| 886 | return FAIL; |
| 887 | } |
| 888 | generate_TYPECHECK(cctx, expected, offset); |
| 889 | return OK; |
| 890 | } |
| 891 | |
| 892 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 893 | * Generate an ISN_PUSHNR instruction. |
| 894 | */ |
| 895 | static int |
| 896 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 897 | { |
| 898 | isn_T *isn; |
| 899 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 900 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 901 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 902 | return FAIL; |
| 903 | isn->isn_arg.number = number; |
| 904 | |
| 905 | return OK; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Generate an ISN_PUSHBOOL instruction. |
| 910 | */ |
| 911 | static int |
| 912 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 913 | { |
| 914 | isn_T *isn; |
| 915 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 916 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 917 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 918 | return FAIL; |
| 919 | isn->isn_arg.number = number; |
| 920 | |
| 921 | return OK; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Generate an ISN_PUSHSPEC instruction. |
| 926 | */ |
| 927 | static int |
| 928 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 929 | { |
| 930 | isn_T *isn; |
| 931 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 932 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 933 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 934 | return FAIL; |
| 935 | isn->isn_arg.number = number; |
| 936 | |
| 937 | return OK; |
| 938 | } |
| 939 | |
| 940 | #ifdef FEAT_FLOAT |
| 941 | /* |
| 942 | * Generate an ISN_PUSHF instruction. |
| 943 | */ |
| 944 | static int |
| 945 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 946 | { |
| 947 | isn_T *isn; |
| 948 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 949 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 950 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 951 | return FAIL; |
| 952 | isn->isn_arg.fnumber = fnumber; |
| 953 | |
| 954 | return OK; |
| 955 | } |
| 956 | #endif |
| 957 | |
| 958 | /* |
| 959 | * Generate an ISN_PUSHS instruction. |
| 960 | * Consumes "str". |
| 961 | */ |
| 962 | static int |
| 963 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 964 | { |
| 965 | isn_T *isn; |
| 966 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 967 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 968 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 969 | return FAIL; |
| 970 | isn->isn_arg.string = str; |
| 971 | |
| 972 | return OK; |
| 973 | } |
| 974 | |
| 975 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 976 | * Generate an ISN_PUSHCHANNEL instruction. |
| 977 | * Consumes "channel". |
| 978 | */ |
| 979 | static int |
| 980 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 981 | { |
| 982 | isn_T *isn; |
| 983 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 984 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 985 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 986 | return FAIL; |
| 987 | isn->isn_arg.channel = channel; |
| 988 | |
| 989 | return OK; |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * Generate an ISN_PUSHJOB instruction. |
| 994 | * Consumes "job". |
| 995 | */ |
| 996 | static int |
| 997 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 998 | { |
| 999 | isn_T *isn; |
| 1000 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1001 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1002 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1003 | return FAIL; |
| 1004 | isn->isn_arg.job = job; |
| 1005 | |
| 1006 | return OK; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1010 | * Generate an ISN_PUSHBLOB instruction. |
| 1011 | * Consumes "blob". |
| 1012 | */ |
| 1013 | static int |
| 1014 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1015 | { |
| 1016 | isn_T *isn; |
| 1017 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1018 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1019 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1020 | return FAIL; |
| 1021 | isn->isn_arg.blob = blob; |
| 1022 | |
| 1023 | return OK; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1027 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1028 | * Consumes "name". |
| 1029 | */ |
| 1030 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1031 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1032 | { |
| 1033 | isn_T *isn; |
| 1034 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1035 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1036 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1037 | return FAIL; |
| 1038 | isn->isn_arg.string = name; |
| 1039 | |
| 1040 | return OK; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 1044 | * Generate a PUSH instruction for "tv". |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 1045 | * "tv" will be consumed or cleared. "tv" may be NULL; |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 1046 | */ |
| 1047 | static int |
| 1048 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1049 | { |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 1050 | if (tv != NULL) |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 1051 | { |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 1052 | switch (tv->v_type) |
| 1053 | { |
| 1054 | case VAR_UNKNOWN: |
| 1055 | break; |
| 1056 | case VAR_BOOL: |
| 1057 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1058 | break; |
| 1059 | case VAR_SPECIAL: |
| 1060 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1061 | break; |
| 1062 | case VAR_NUMBER: |
| 1063 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1064 | break; |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 1065 | #ifdef FEAT_FLOAT |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 1066 | case VAR_FLOAT: |
| 1067 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1068 | break; |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 1069 | #endif |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 1070 | case VAR_BLOB: |
| 1071 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1072 | tv->vval.v_blob = NULL; |
| 1073 | break; |
| 1074 | case VAR_STRING: |
| 1075 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1076 | tv->vval.v_string = NULL; |
| 1077 | break; |
| 1078 | default: |
| 1079 | iemsg("constant type not supported"); |
| 1080 | clear_tv(tv); |
| 1081 | return FAIL; |
| 1082 | } |
| 1083 | tv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 1084 | } |
| 1085 | return OK; |
| 1086 | } |
| 1087 | |
| 1088 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1089 | * Generate an ISN_STORE instruction. |
| 1090 | */ |
| 1091 | static int |
| 1092 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1093 | { |
| 1094 | isn_T *isn; |
| 1095 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1096 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1097 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1098 | return FAIL; |
| 1099 | if (name != NULL) |
| 1100 | isn->isn_arg.string = vim_strsave(name); |
| 1101 | else |
| 1102 | isn->isn_arg.number = idx; |
| 1103 | |
| 1104 | return OK; |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1109 | */ |
| 1110 | static int |
| 1111 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1112 | { |
| 1113 | isn_T *isn; |
| 1114 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1115 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1116 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1117 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1118 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1119 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1120 | |
| 1121 | return OK; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Generate an ISN_STOREOPT instruction |
| 1126 | */ |
| 1127 | static int |
| 1128 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1129 | { |
| 1130 | isn_T *isn; |
| 1131 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1132 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1133 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1134 | return FAIL; |
| 1135 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1136 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1137 | |
| 1138 | return OK; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Generate an ISN_LOAD or similar instruction. |
| 1143 | */ |
| 1144 | static int |
| 1145 | generate_LOAD( |
| 1146 | cctx_T *cctx, |
| 1147 | isntype_T isn_type, |
| 1148 | int idx, |
| 1149 | char_u *name, |
| 1150 | type_T *type) |
| 1151 | { |
| 1152 | isn_T *isn; |
| 1153 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1154 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1155 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1156 | return FAIL; |
| 1157 | if (name != NULL) |
| 1158 | isn->isn_arg.string = vim_strsave(name); |
| 1159 | else |
| 1160 | isn->isn_arg.number = idx; |
| 1161 | |
| 1162 | return OK; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1166 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1167 | */ |
| 1168 | static int |
| 1169 | generate_LOADV( |
| 1170 | cctx_T *cctx, |
| 1171 | char_u *name, |
| 1172 | int error) |
| 1173 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1174 | int di_flags; |
| 1175 | int vidx = find_vim_var(name, &di_flags); |
| 1176 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1177 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1178 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1179 | if (vidx < 0) |
| 1180 | { |
| 1181 | if (error) |
| 1182 | semsg(_(e_var_notfound), name); |
| 1183 | return FAIL; |
| 1184 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1185 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1186 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1187 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1191 | * Generate an ISN_UNLET instruction. |
| 1192 | */ |
| 1193 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1194 | 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] | 1195 | { |
| 1196 | isn_T *isn; |
| 1197 | |
| 1198 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1199 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1200 | return FAIL; |
| 1201 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1202 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1203 | |
| 1204 | return OK; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1208 | * Generate an ISN_LOADS instruction. |
| 1209 | */ |
| 1210 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1211 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1212 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1213 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1214 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1215 | int sid, |
| 1216 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1217 | { |
| 1218 | isn_T *isn; |
| 1219 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1220 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1221 | if (isn_type == ISN_LOADS) |
| 1222 | isn = generate_instr_type(cctx, isn_type, type); |
| 1223 | else |
| 1224 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1225 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1226 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1227 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1228 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1229 | |
| 1230 | return OK; |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1235 | */ |
| 1236 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1237 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1238 | cctx_T *cctx, |
| 1239 | isntype_T isn_type, |
| 1240 | int sid, |
| 1241 | int idx, |
| 1242 | type_T *type) |
| 1243 | { |
| 1244 | isn_T *isn; |
| 1245 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1246 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1247 | if (isn_type == ISN_LOADSCRIPT) |
| 1248 | isn = generate_instr_type(cctx, isn_type, type); |
| 1249 | else |
| 1250 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1251 | if (isn == NULL) |
| 1252 | return FAIL; |
| 1253 | isn->isn_arg.script.script_sid = sid; |
| 1254 | isn->isn_arg.script.script_idx = idx; |
| 1255 | return OK; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Generate an ISN_NEWLIST instruction. |
| 1260 | */ |
| 1261 | static int |
| 1262 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1263 | { |
| 1264 | isn_T *isn; |
| 1265 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1266 | type_T *type; |
| 1267 | type_T *member; |
| 1268 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1269 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1270 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1271 | return FAIL; |
| 1272 | isn->isn_arg.number = count; |
| 1273 | |
| 1274 | // drop the value types |
| 1275 | stack->ga_len -= count; |
| 1276 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1277 | // Use the first value type for the list member type. Use "any" for an |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1278 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1279 | if (count > 0) |
| 1280 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1281 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1282 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1283 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1284 | |
| 1285 | // add the list type to the type stack |
| 1286 | if (ga_grow(stack, 1) == FAIL) |
| 1287 | return FAIL; |
| 1288 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1289 | ++stack->ga_len; |
| 1290 | |
| 1291 | return OK; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Generate an ISN_NEWDICT instruction. |
| 1296 | */ |
| 1297 | static int |
| 1298 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1299 | { |
| 1300 | isn_T *isn; |
| 1301 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1302 | type_T *type; |
| 1303 | type_T *member; |
| 1304 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1305 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1306 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1307 | return FAIL; |
| 1308 | isn->isn_arg.number = count; |
| 1309 | |
| 1310 | // drop the key and value types |
| 1311 | stack->ga_len -= 2 * count; |
| 1312 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1313 | // Use the first value type for the list member type. Use "void" for an |
| 1314 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1315 | if (count > 0) |
| 1316 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1317 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1318 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1319 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1320 | |
| 1321 | // add the dict type to the type stack |
| 1322 | if (ga_grow(stack, 1) == FAIL) |
| 1323 | return FAIL; |
| 1324 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1325 | ++stack->ga_len; |
| 1326 | |
| 1327 | return OK; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * Generate an ISN_FUNCREF instruction. |
| 1332 | */ |
| 1333 | static int |
| 1334 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1335 | { |
| 1336 | isn_T *isn; |
| 1337 | garray_T *stack = &cctx->ctx_type_stack; |
| 1338 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1339 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1340 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1341 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1342 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1343 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1344 | |
| 1345 | if (ga_grow(stack, 1) == FAIL) |
| 1346 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1347 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1348 | // TODO: argument and return types |
| 1349 | ++stack->ga_len; |
| 1350 | |
| 1351 | return OK; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Generate an ISN_JUMP instruction. |
| 1356 | */ |
| 1357 | static int |
| 1358 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1359 | { |
| 1360 | isn_T *isn; |
| 1361 | garray_T *stack = &cctx->ctx_type_stack; |
| 1362 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1363 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1364 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1365 | return FAIL; |
| 1366 | isn->isn_arg.jump.jump_when = when; |
| 1367 | isn->isn_arg.jump.jump_where = where; |
| 1368 | |
| 1369 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1370 | --stack->ga_len; |
| 1371 | |
| 1372 | return OK; |
| 1373 | } |
| 1374 | |
| 1375 | static int |
| 1376 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1377 | { |
| 1378 | isn_T *isn; |
| 1379 | garray_T *stack = &cctx->ctx_type_stack; |
| 1380 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1381 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1382 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1383 | return FAIL; |
| 1384 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1385 | |
| 1386 | if (ga_grow(stack, 1) == FAIL) |
| 1387 | return FAIL; |
| 1388 | // type doesn't matter, will be stored next |
| 1389 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1390 | ++stack->ga_len; |
| 1391 | |
| 1392 | return OK; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Generate an ISN_BCALL instruction. |
| 1397 | * Return FAIL if the number of arguments is wrong. |
| 1398 | */ |
| 1399 | static int |
| 1400 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount) |
| 1401 | { |
| 1402 | isn_T *isn; |
| 1403 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1404 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1405 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1406 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1407 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1408 | if (check_internal_func(func_idx, argcount) == FAIL) |
| 1409 | return FAIL; |
| 1410 | |
| 1411 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1412 | return FAIL; |
| 1413 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1414 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1415 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1416 | for (i = 0; i < argcount; ++i) |
| 1417 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1418 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1419 | stack->ga_len -= argcount; // drop the arguments |
| 1420 | if (ga_grow(stack, 1) == FAIL) |
| 1421 | return FAIL; |
| 1422 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1423 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1424 | ++stack->ga_len; // add return value |
| 1425 | |
| 1426 | return OK; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1431 | * Return FAIL if the number of arguments is wrong. |
| 1432 | */ |
| 1433 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1434 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1435 | { |
| 1436 | isn_T *isn; |
| 1437 | garray_T *stack = &cctx->ctx_type_stack; |
| 1438 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1439 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1440 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1441 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1442 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1443 | { |
| 1444 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1445 | return FAIL; |
| 1446 | } |
| 1447 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1448 | { |
| 1449 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1450 | return FAIL; |
| 1451 | } |
| 1452 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1453 | if (ufunc->uf_dfunc_idx >= 0) |
| 1454 | { |
| 1455 | int i; |
| 1456 | |
| 1457 | for (i = 0; i < argcount; ++i) |
| 1458 | { |
| 1459 | type_T *expected; |
| 1460 | type_T *actual; |
| 1461 | |
| 1462 | if (i < regular_args) |
| 1463 | { |
| 1464 | if (ufunc->uf_arg_types == NULL) |
| 1465 | continue; |
| 1466 | expected = ufunc->uf_arg_types[i]; |
| 1467 | } |
| 1468 | else |
| 1469 | expected = ufunc->uf_va_type->tt_member; |
| 1470 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1471 | if (need_type(actual, expected, -argcount + i, cctx) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1472 | { |
| 1473 | arg_type_mismatch(expected, actual, i + 1); |
| 1474 | return FAIL; |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1479 | if ((isn = generate_instr(cctx, |
| 1480 | ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL) |
| 1481 | return FAIL; |
| 1482 | if (ufunc->uf_dfunc_idx >= 0) |
| 1483 | { |
| 1484 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1485 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | // A user function may be deleted and redefined later, can't use the |
| 1490 | // ufunc pointer, need to look it up again at runtime. |
| 1491 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1492 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1493 | } |
| 1494 | |
| 1495 | stack->ga_len -= argcount; // drop the arguments |
| 1496 | if (ga_grow(stack, 1) == FAIL) |
| 1497 | return FAIL; |
| 1498 | // add return value |
| 1499 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1500 | ++stack->ga_len; |
| 1501 | |
| 1502 | return OK; |
| 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1507 | */ |
| 1508 | static int |
| 1509 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1510 | { |
| 1511 | isn_T *isn; |
| 1512 | garray_T *stack = &cctx->ctx_type_stack; |
| 1513 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1514 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1515 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1516 | return FAIL; |
| 1517 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1518 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1519 | |
| 1520 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1521 | if (ga_grow(stack, 1) == FAIL) |
| 1522 | return FAIL; |
| 1523 | // add return value |
| 1524 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1525 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1526 | |
| 1527 | return OK; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1532 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1533 | */ |
| 1534 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1535 | generate_PCALL( |
| 1536 | cctx_T *cctx, |
| 1537 | int argcount, |
| 1538 | char_u *name, |
| 1539 | type_T *type, |
| 1540 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1541 | { |
| 1542 | isn_T *isn; |
| 1543 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1544 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1546 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1547 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1548 | if (type->tt_type == VAR_ANY) |
| 1549 | ret_type = &t_any; |
| 1550 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1551 | { |
| 1552 | if (type->tt_argcount != -1) |
| 1553 | { |
| 1554 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1555 | |
| 1556 | if (argcount < type->tt_min_argcount - varargs) |
| 1557 | { |
| 1558 | semsg(_(e_toofewarg), "[reference]"); |
| 1559 | return FAIL; |
| 1560 | } |
| 1561 | if (!varargs && argcount > type->tt_argcount) |
| 1562 | { |
| 1563 | semsg(_(e_toomanyarg), "[reference]"); |
| 1564 | return FAIL; |
| 1565 | } |
| 1566 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1567 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1568 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1569 | else |
| 1570 | { |
| 1571 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1572 | return FAIL; |
| 1573 | } |
| 1574 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1575 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1576 | return FAIL; |
| 1577 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1578 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1579 | |
| 1580 | stack->ga_len -= argcount; // drop the arguments |
| 1581 | |
| 1582 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1583 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1584 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1585 | // If partial is above the arguments it must be cleared and replaced with |
| 1586 | // the return value. |
| 1587 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1588 | return FAIL; |
| 1589 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1590 | return OK; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * Generate an ISN_MEMBER instruction. |
| 1595 | */ |
| 1596 | static int |
| 1597 | generate_MEMBER(cctx_T *cctx, char_u *name, size_t len) |
| 1598 | { |
| 1599 | isn_T *isn; |
| 1600 | garray_T *stack = &cctx->ctx_type_stack; |
| 1601 | type_T *type; |
| 1602 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1603 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1604 | if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL) |
| 1605 | return FAIL; |
| 1606 | isn->isn_arg.string = vim_strnsave(name, (int)len); |
| 1607 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1608 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1609 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1610 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1611 | { |
| 1612 | emsg(_(e_dictreq)); |
| 1613 | return FAIL; |
| 1614 | } |
| 1615 | // change dict type to dict member type |
| 1616 | if (type->tt_type == VAR_DICT) |
| 1617 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1618 | |
| 1619 | return OK; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * Generate an ISN_ECHO instruction. |
| 1624 | */ |
| 1625 | static int |
| 1626 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1627 | { |
| 1628 | isn_T *isn; |
| 1629 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1630 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1631 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1632 | return FAIL; |
| 1633 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1634 | isn->isn_arg.echo.echo_count = count; |
| 1635 | |
| 1636 | return OK; |
| 1637 | } |
| 1638 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1639 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1640 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1641 | */ |
| 1642 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1643 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1644 | { |
| 1645 | isn_T *isn; |
| 1646 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1647 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1648 | return FAIL; |
| 1649 | isn->isn_arg.number = count; |
| 1650 | |
| 1651 | return OK; |
| 1652 | } |
| 1653 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1654 | static int |
| 1655 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1656 | { |
| 1657 | isn_T *isn; |
| 1658 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1659 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1660 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1661 | return FAIL; |
| 1662 | isn->isn_arg.string = vim_strsave(line); |
| 1663 | return OK; |
| 1664 | } |
| 1665 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1666 | static int |
| 1667 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1668 | { |
| 1669 | isn_T *isn; |
| 1670 | |
| 1671 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1672 | return FAIL; |
| 1673 | isn->isn_arg.number = count; |
| 1674 | return OK; |
| 1675 | } |
| 1676 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1677 | /* |
| 1678 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1679 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1680 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1681 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1682 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1683 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1684 | lvar_T *lvar; |
| 1685 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1686 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1687 | { |
| 1688 | emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1689 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1693 | return NULL; |
| 1694 | 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] | 1695 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1696 | // Every local variable uses the next entry on the stack. We could re-use |
| 1697 | // the last ones when leaving a scope, but then variables used in a closure |
| 1698 | // might get overwritten. To keep things simple do not re-use stack |
| 1699 | // entries. This is less efficient, but memory is cheap these days. |
| 1700 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1701 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1702 | lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len)); |
| 1703 | lvar->lv_const = isConst; |
| 1704 | lvar->lv_type = type; |
| 1705 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1706 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1710 | * Remove local variables above "new_top". |
| 1711 | */ |
| 1712 | static void |
| 1713 | unwind_locals(cctx_T *cctx, int new_top) |
| 1714 | { |
| 1715 | if (cctx->ctx_locals.ga_len > new_top) |
| 1716 | { |
| 1717 | int idx; |
| 1718 | lvar_T *lvar; |
| 1719 | |
| 1720 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1721 | { |
| 1722 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1723 | vim_free(lvar->lv_name); |
| 1724 | } |
| 1725 | } |
| 1726 | cctx->ctx_locals.ga_len = new_top; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * Free all local variables. |
| 1731 | */ |
| 1732 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1733 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1734 | { |
| 1735 | unwind_locals(cctx, 0); |
| 1736 | ga_clear(&cctx->ctx_locals); |
| 1737 | } |
| 1738 | |
| 1739 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1740 | * Skip over a type definition and return a pointer to just after it. |
| 1741 | */ |
| 1742 | char_u * |
| 1743 | skip_type(char_u *start) |
| 1744 | { |
| 1745 | char_u *p = start; |
| 1746 | |
| 1747 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1748 | ++p; |
| 1749 | |
| 1750 | // Skip over "<type>"; this is permissive about white space. |
| 1751 | if (*skipwhite(p) == '<') |
| 1752 | { |
| 1753 | p = skipwhite(p); |
| 1754 | p = skip_type(skipwhite(p + 1)); |
| 1755 | p = skipwhite(p); |
| 1756 | if (*p == '>') |
| 1757 | ++p; |
| 1758 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1759 | else if (*p == '(' && STRNCMP("func", start, 4) == 0) |
| 1760 | { |
| 1761 | // handle func(args): type |
| 1762 | ++p; |
| 1763 | while (*p != ')' && *p != NUL) |
| 1764 | { |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1765 | char_u *sp = p; |
| 1766 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1767 | p = skip_type(p); |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1768 | if (p == sp) |
| 1769 | return p; // syntax error |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1770 | if (*p == ',') |
| 1771 | p = skipwhite(p + 1); |
| 1772 | } |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1773 | if (*p == ')') |
| 1774 | { |
| 1775 | if (p[1] == ':') |
| 1776 | p = skip_type(skipwhite(p + 2)); |
| 1777 | else |
| 1778 | p = skipwhite(p + 1); |
| 1779 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1780 | } |
| 1781 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1782 | return p; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1787 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1788 | * Returns NULL in case of failure. |
| 1789 | */ |
| 1790 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1791 | parse_type_member(char_u **arg, type_T *type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1792 | { |
| 1793 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1794 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1795 | |
| 1796 | if (**arg != '<') |
| 1797 | { |
| 1798 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1799 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1800 | else |
| 1801 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1802 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1803 | } |
| 1804 | *arg = skipwhite(*arg + 1); |
| 1805 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1806 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1807 | |
| 1808 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1809 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1810 | { |
| 1811 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1812 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1813 | } |
| 1814 | ++*arg; |
| 1815 | |
| 1816 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1817 | return get_list_type(member_type, type_gap); |
| 1818 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1823 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1824 | */ |
| 1825 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1826 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1827 | { |
| 1828 | char_u *p = *arg; |
| 1829 | size_t len; |
| 1830 | |
| 1831 | // skip over the first word |
| 1832 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1833 | ++p; |
| 1834 | len = p - *arg; |
| 1835 | |
| 1836 | switch (**arg) |
| 1837 | { |
| 1838 | case 'a': |
| 1839 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1840 | { |
| 1841 | *arg += len; |
| 1842 | return &t_any; |
| 1843 | } |
| 1844 | break; |
| 1845 | case 'b': |
| 1846 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1847 | { |
| 1848 | *arg += len; |
| 1849 | return &t_bool; |
| 1850 | } |
| 1851 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1852 | { |
| 1853 | *arg += len; |
| 1854 | return &t_blob; |
| 1855 | } |
| 1856 | break; |
| 1857 | case 'c': |
| 1858 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1859 | { |
| 1860 | *arg += len; |
| 1861 | return &t_channel; |
| 1862 | } |
| 1863 | break; |
| 1864 | case 'd': |
| 1865 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1866 | { |
| 1867 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1868 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1869 | } |
| 1870 | break; |
| 1871 | case 'f': |
| 1872 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1873 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1874 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1875 | *arg += len; |
| 1876 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1877 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1878 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1879 | return &t_any; |
| 1880 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1881 | } |
| 1882 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 1883 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1884 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1885 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1886 | int argcount = -1; |
| 1887 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1888 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1889 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 1890 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1891 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1892 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1893 | if (**arg == '(') |
| 1894 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1895 | // "func" may or may not return a value, "func()" does |
| 1896 | // not return a value. |
| 1897 | ret_type = &t_void; |
| 1898 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1899 | p = ++*arg; |
| 1900 | argcount = 0; |
| 1901 | while (*p != NUL && *p != ')') |
| 1902 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1903 | if (*p == '?') |
| 1904 | { |
| 1905 | if (first_optional == -1) |
| 1906 | first_optional = argcount; |
| 1907 | ++p; |
| 1908 | } |
| 1909 | else if (first_optional != -1) |
| 1910 | { |
| 1911 | emsg(_("E1007: mandatory argument after optional argument")); |
| 1912 | return &t_any; |
| 1913 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1914 | else if (STRNCMP(p, "...", 3) == 0) |
| 1915 | { |
| 1916 | flags |= TTFLAG_VARARGS; |
| 1917 | p += 3; |
| 1918 | } |
| 1919 | |
| 1920 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 1921 | |
| 1922 | // Nothing comes after "...{type}". |
| 1923 | if (flags & TTFLAG_VARARGS) |
| 1924 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1925 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1926 | if (*p != ',' && *skipwhite(p) == ',') |
| 1927 | { |
| 1928 | semsg(_(e_no_white_before), ","); |
| 1929 | return &t_any; |
| 1930 | } |
| 1931 | if (*p == ',') |
| 1932 | { |
| 1933 | ++p; |
| 1934 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1935 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1936 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1937 | return &t_any; |
| 1938 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1939 | } |
| 1940 | p = skipwhite(p); |
| 1941 | if (argcount == MAX_FUNC_ARGS) |
| 1942 | { |
| 1943 | emsg(_("E740: Too many argument types")); |
| 1944 | return &t_any; |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | p = skipwhite(p); |
| 1949 | if (*p != ')') |
| 1950 | { |
| 1951 | emsg(_(e_missing_close)); |
| 1952 | return &t_any; |
| 1953 | } |
| 1954 | *arg = p + 1; |
| 1955 | } |
| 1956 | if (**arg == ':') |
| 1957 | { |
| 1958 | // parse return type |
| 1959 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1960 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1961 | semsg(_(e_white_after), ":"); |
| 1962 | *arg = skipwhite(*arg); |
| 1963 | ret_type = parse_type(arg, type_gap); |
| 1964 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1965 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1966 | type = get_func_type(ret_type, argcount, type_gap); |
| 1967 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1968 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1969 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 1970 | type->tt_flags = flags; |
| 1971 | if (argcount > 0) |
| 1972 | { |
| 1973 | type->tt_argcount = argcount; |
| 1974 | type->tt_min_argcount = first_optional == -1 |
| 1975 | ? argcount : first_optional; |
| 1976 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1977 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1978 | return &t_any; |
| 1979 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1980 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1981 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1982 | } |
| 1983 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1984 | } |
| 1985 | break; |
| 1986 | case 'j': |
| 1987 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 1988 | { |
| 1989 | *arg += len; |
| 1990 | return &t_job; |
| 1991 | } |
| 1992 | break; |
| 1993 | case 'l': |
| 1994 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 1995 | { |
| 1996 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1997 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1998 | } |
| 1999 | break; |
| 2000 | case 'n': |
| 2001 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2002 | { |
| 2003 | *arg += len; |
| 2004 | return &t_number; |
| 2005 | } |
| 2006 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2007 | case 's': |
| 2008 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2009 | { |
| 2010 | *arg += len; |
| 2011 | return &t_string; |
| 2012 | } |
| 2013 | break; |
| 2014 | case 'v': |
| 2015 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2016 | { |
| 2017 | *arg += len; |
| 2018 | return &t_void; |
| 2019 | } |
| 2020 | break; |
| 2021 | } |
| 2022 | |
| 2023 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2024 | return &t_any; |
| 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | * Check if "type1" and "type2" are exactly the same. |
| 2029 | */ |
| 2030 | static int |
| 2031 | equal_type(type_T *type1, type_T *type2) |
| 2032 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2033 | int i; |
| 2034 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2035 | if (type1->tt_type != type2->tt_type) |
| 2036 | return FALSE; |
| 2037 | switch (type1->tt_type) |
| 2038 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2039 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2040 | case VAR_ANY: |
| 2041 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2042 | case VAR_SPECIAL: |
| 2043 | case VAR_BOOL: |
| 2044 | case VAR_NUMBER: |
| 2045 | case VAR_FLOAT: |
| 2046 | case VAR_STRING: |
| 2047 | case VAR_BLOB: |
| 2048 | case VAR_JOB: |
| 2049 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2050 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2051 | case VAR_LIST: |
| 2052 | case VAR_DICT: |
| 2053 | return equal_type(type1->tt_member, type2->tt_member); |
| 2054 | case VAR_FUNC: |
| 2055 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2056 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2057 | || type1->tt_argcount != type2->tt_argcount) |
| 2058 | return FALSE; |
| 2059 | if (type1->tt_argcount < 0 |
| 2060 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2061 | return TRUE; |
| 2062 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2063 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2064 | return FALSE; |
| 2065 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2066 | } |
| 2067 | return TRUE; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2072 | * "type2" and "dest" may be the same. |
| 2073 | */ |
| 2074 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2075 | common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2076 | { |
| 2077 | if (equal_type(type1, type2)) |
| 2078 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2079 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2080 | return; |
| 2081 | } |
| 2082 | |
| 2083 | if (type1->tt_type == type2->tt_type) |
| 2084 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2085 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2086 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2087 | type_T *common; |
| 2088 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2089 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2090 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2091 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2092 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2093 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2094 | return; |
| 2095 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2096 | if (type1->tt_type == VAR_FUNC) |
| 2097 | { |
| 2098 | type_T *common; |
| 2099 | |
| 2100 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2101 | if (type1->tt_argcount == type2->tt_argcount |
| 2102 | && type1->tt_argcount >= 0) |
| 2103 | { |
| 2104 | int argcount = type1->tt_argcount; |
| 2105 | int i; |
| 2106 | |
| 2107 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2108 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2109 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2110 | if (func_type_add_arg_types(*dest, argcount, |
| 2111 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2112 | for (i = 0; i < argcount; ++i) |
| 2113 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2114 | &(*dest)->tt_args[i], type_gap); |
| 2115 | } |
| 2116 | } |
| 2117 | else |
| 2118 | *dest = alloc_func_type(common, -1, type_gap); |
| 2119 | return; |
| 2120 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2123 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | char * |
| 2127 | vartype_name(vartype_T type) |
| 2128 | { |
| 2129 | switch (type) |
| 2130 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2131 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2132 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2133 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2134 | case VAR_SPECIAL: return "special"; |
| 2135 | case VAR_BOOL: return "bool"; |
| 2136 | case VAR_NUMBER: return "number"; |
| 2137 | case VAR_FLOAT: return "float"; |
| 2138 | case VAR_STRING: return "string"; |
| 2139 | case VAR_BLOB: return "blob"; |
| 2140 | case VAR_JOB: return "job"; |
| 2141 | case VAR_CHANNEL: return "channel"; |
| 2142 | case VAR_LIST: return "list"; |
| 2143 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2144 | |
| 2145 | case VAR_FUNC: |
| 2146 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2147 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2148 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* |
| 2152 | * Return the name of a type. |
| 2153 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2154 | */ |
| 2155 | char * |
| 2156 | type_name(type_T *type, char **tofree) |
| 2157 | { |
| 2158 | char *name = vartype_name(type->tt_type); |
| 2159 | |
| 2160 | *tofree = NULL; |
| 2161 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2162 | { |
| 2163 | char *member_free; |
| 2164 | char *member_name = type_name(type->tt_member, &member_free); |
| 2165 | size_t len; |
| 2166 | |
| 2167 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2168 | *tofree = alloc(len); |
| 2169 | if (*tofree != NULL) |
| 2170 | { |
| 2171 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2172 | vim_free(member_free); |
| 2173 | return *tofree; |
| 2174 | } |
| 2175 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2176 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2177 | { |
| 2178 | garray_T ga; |
| 2179 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2180 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2181 | |
| 2182 | ga_init2(&ga, 1, 100); |
| 2183 | if (ga_grow(&ga, 20) == FAIL) |
| 2184 | return "[unknown]"; |
| 2185 | *tofree = ga.ga_data; |
| 2186 | STRCPY(ga.ga_data, "func("); |
| 2187 | ga.ga_len += 5; |
| 2188 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2189 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2190 | { |
| 2191 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2192 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2193 | int len; |
| 2194 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2195 | if (type->tt_args == NULL) |
| 2196 | arg_type = "[unknown]"; |
| 2197 | else |
| 2198 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2199 | if (i > 0) |
| 2200 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2201 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2202 | ga.ga_len += 2; |
| 2203 | } |
| 2204 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2205 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2206 | { |
| 2207 | vim_free(arg_free); |
| 2208 | return "[unknown]"; |
| 2209 | } |
| 2210 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2211 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2212 | { |
| 2213 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2214 | ga.ga_len += 3; |
| 2215 | } |
| 2216 | else if (i >= type->tt_min_argcount) |
| 2217 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2218 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2219 | ga.ga_len += len; |
| 2220 | vim_free(arg_free); |
| 2221 | } |
| 2222 | |
| 2223 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2224 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2225 | else |
| 2226 | { |
| 2227 | char *ret_free; |
| 2228 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2229 | int len; |
| 2230 | |
| 2231 | len = (int)STRLEN(ret_name) + 4; |
| 2232 | if (ga_grow(&ga, len) == FAIL) |
| 2233 | { |
| 2234 | vim_free(ret_free); |
| 2235 | return "[unknown]"; |
| 2236 | } |
| 2237 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2238 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2239 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2240 | vim_free(ret_free); |
| 2241 | } |
| 2242 | return ga.ga_data; |
| 2243 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2244 | |
| 2245 | return name; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * Find "name" in script-local items of script "sid". |
| 2250 | * Returns the index in "sn_var_vals" if found. |
| 2251 | * If found but not in "sn_var_vals" returns -1. |
| 2252 | * If not found returns -2. |
| 2253 | */ |
| 2254 | int |
| 2255 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2256 | { |
| 2257 | hashtab_T *ht; |
| 2258 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2259 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2260 | int idx; |
| 2261 | |
| 2262 | // First look the name up in the hashtable. |
| 2263 | if (sid <= 0 || sid > script_items.ga_len) |
| 2264 | return -1; |
| 2265 | ht = &SCRIPT_VARS(sid); |
| 2266 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2267 | if (di == NULL) |
| 2268 | return -2; |
| 2269 | |
| 2270 | // Now find the svar_T index in sn_var_vals. |
| 2271 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2272 | { |
| 2273 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2274 | |
| 2275 | if (sv->sv_tv == &di->di_tv) |
| 2276 | { |
| 2277 | if (check_writable && sv->sv_const) |
| 2278 | semsg(_(e_readonlyvar), name); |
| 2279 | return idx; |
| 2280 | } |
| 2281 | } |
| 2282 | return -1; |
| 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * Find "name" in imported items of the current script/ |
| 2287 | */ |
| 2288 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2289 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2290 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2291 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2292 | int idx; |
| 2293 | |
| 2294 | if (cctx != NULL) |
| 2295 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2296 | { |
| 2297 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2298 | + idx; |
| 2299 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2300 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2301 | : STRLEN(import->imp_name) == len |
| 2302 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2303 | return import; |
| 2304 | } |
| 2305 | |
| 2306 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2307 | { |
| 2308 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2309 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2310 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2311 | : STRLEN(import->imp_name) == len |
| 2312 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2313 | return import; |
| 2314 | } |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | |
| 2318 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2319 | * Free all imported variables. |
| 2320 | */ |
| 2321 | static void |
| 2322 | free_imported(cctx_T *cctx) |
| 2323 | { |
| 2324 | int idx; |
| 2325 | |
| 2326 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2327 | { |
| 2328 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2329 | |
| 2330 | vim_free(import->imp_name); |
| 2331 | } |
| 2332 | ga_clear(&cctx->ctx_imports); |
| 2333 | } |
| 2334 | |
| 2335 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2336 | * Get the next line of the function from "cctx". |
| 2337 | * Returns NULL when at the end. |
| 2338 | */ |
| 2339 | static char_u * |
| 2340 | next_line_from_context(cctx_T *cctx) |
| 2341 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2342 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2343 | |
| 2344 | do |
| 2345 | { |
| 2346 | ++cctx->ctx_lnum; |
| 2347 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2348 | { |
| 2349 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2350 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2351 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2352 | 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] | 2353 | cctx->ctx_line_start = line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2354 | SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum |
| 2355 | + cctx->ctx_lnum + 1; |
Bram Moolenaar | 675f716 | 2020-04-12 22:53:54 +0200 | [diff] [blame] | 2356 | } while (line == NULL || *skipwhite(line) == NUL); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2357 | return line; |
| 2358 | } |
| 2359 | |
| 2360 | /* |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2361 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2362 | */ |
| 2363 | static int |
| 2364 | comment_start(char_u *p) |
| 2365 | { |
| 2366 | return p[0] == '#' && p[1] != '{'; |
| 2367 | } |
| 2368 | |
| 2369 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2370 | * 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] | 2371 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2372 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2373 | */ |
| 2374 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2375 | 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] | 2376 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2377 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2378 | { |
| 2379 | char_u *next = next_line_from_context(cctx); |
| 2380 | |
| 2381 | if (next == NULL) |
| 2382 | return FAIL; |
| 2383 | *arg = skipwhite(next); |
| 2384 | } |
| 2385 | return OK; |
| 2386 | } |
| 2387 | |
| 2388 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2389 | * Generate an instruction to load script-local variable "name", without the |
| 2390 | * leading "s:". |
| 2391 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2392 | */ |
| 2393 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2394 | compile_load_scriptvar( |
| 2395 | cctx_T *cctx, |
| 2396 | char_u *name, // variable NUL terminated |
| 2397 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2398 | char_u **end, // end of variable |
| 2399 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2400 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2401 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2402 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2403 | imported_T *import; |
| 2404 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2405 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2406 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2407 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2408 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2409 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2410 | } |
| 2411 | if (idx >= 0) |
| 2412 | { |
| 2413 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2414 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2415 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2416 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2417 | return OK; |
| 2418 | } |
| 2419 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2420 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2421 | if (import != NULL) |
| 2422 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2423 | if (import->imp_all) |
| 2424 | { |
| 2425 | char_u *p = skipwhite(*end); |
| 2426 | int name_len; |
| 2427 | ufunc_T *ufunc; |
| 2428 | type_T *type; |
| 2429 | |
| 2430 | // Used "import * as Name", need to lookup the member. |
| 2431 | if (*p != '.') |
| 2432 | { |
| 2433 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2434 | return FAIL; |
| 2435 | } |
| 2436 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2437 | if (VIM_ISWHITE(*p)) |
| 2438 | { |
| 2439 | emsg(_("E1074: no white space allowed after dot")); |
| 2440 | return FAIL; |
| 2441 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2442 | |
| 2443 | idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type); |
| 2444 | // TODO: what if it is a function? |
| 2445 | if (idx < 0) |
| 2446 | return FAIL; |
| 2447 | *end = p; |
| 2448 | |
| 2449 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2450 | import->imp_sid, |
| 2451 | idx, |
| 2452 | type); |
| 2453 | } |
| 2454 | else |
| 2455 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2456 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2457 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2458 | import->imp_sid, |
| 2459 | import->imp_var_vals_idx, |
| 2460 | import->imp_type); |
| 2461 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2462 | return OK; |
| 2463 | } |
| 2464 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2465 | if (error) |
| 2466 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2467 | return FAIL; |
| 2468 | } |
| 2469 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2470 | static int |
| 2471 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2472 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2473 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2474 | |
| 2475 | if (ufunc == NULL) |
| 2476 | return FAIL; |
| 2477 | |
| 2478 | return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type); |
| 2479 | } |
| 2480 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2481 | /* |
| 2482 | * Compile a variable name into a load instruction. |
| 2483 | * "end" points to just after the name. |
| 2484 | * When "error" is FALSE do not give an error when not found. |
| 2485 | */ |
| 2486 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2487 | 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] | 2488 | { |
| 2489 | type_T *type; |
| 2490 | char_u *name; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2491 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2492 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2493 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2494 | |
| 2495 | if (*(*arg + 1) == ':') |
| 2496 | { |
| 2497 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2498 | if (end <= *arg + 2) |
| 2499 | name = vim_strsave((char_u *)"[empty]"); |
| 2500 | else |
| 2501 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2502 | if (name == NULL) |
| 2503 | return FAIL; |
| 2504 | |
| 2505 | if (**arg == 'v') |
| 2506 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2507 | res = generate_LOADV(cctx, name, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2508 | } |
| 2509 | else if (**arg == 'g') |
| 2510 | { |
| 2511 | // Global variables can be defined later, thus we don't check if it |
| 2512 | // exists, give error at runtime. |
| 2513 | res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); |
| 2514 | } |
| 2515 | else if (**arg == 's') |
| 2516 | { |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2517 | res = compile_load_scriptvar(cctx, name, NULL, NULL, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2518 | } |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2519 | else if (**arg == 'b') |
| 2520 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2521 | // Buffer-local variables can be defined later, thus we don't check |
| 2522 | // if it exists, give error at runtime. |
| 2523 | res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2524 | } |
| 2525 | else if (**arg == 'w') |
| 2526 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2527 | // Window-local variables can be defined later, thus we don't check |
| 2528 | // if it exists, give error at runtime. |
| 2529 | res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2530 | } |
| 2531 | else if (**arg == 't') |
| 2532 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2533 | // Tabpage-local variables can be defined later, thus we don't |
| 2534 | // check if it exists, give error at runtime. |
| 2535 | res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2536 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2537 | else |
| 2538 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2539 | semsg("E1075: Namespace not supported: %s", *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2540 | goto theend; |
| 2541 | } |
| 2542 | } |
| 2543 | else |
| 2544 | { |
| 2545 | size_t len = end - *arg; |
| 2546 | int idx; |
| 2547 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2548 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2549 | |
| 2550 | name = vim_strnsave(*arg, end - *arg); |
| 2551 | if (name == NULL) |
| 2552 | return FAIL; |
| 2553 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2554 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2555 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2556 | if (!gen_load_outer) |
| 2557 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2558 | } |
| 2559 | else |
| 2560 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2561 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2562 | |
| 2563 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2564 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2565 | type = lvar->lv_type; |
| 2566 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2567 | if (lvar->lv_from_outer) |
| 2568 | gen_load_outer = TRUE; |
| 2569 | else |
| 2570 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2571 | } |
| 2572 | else |
| 2573 | { |
| 2574 | if ((len == 4 && STRNCMP("true", *arg, 4) == 0) |
| 2575 | || (len == 5 && STRNCMP("false", *arg, 5) == 0)) |
| 2576 | res = generate_PUSHBOOL(cctx, **arg == 't' |
| 2577 | ? VVAL_TRUE : VVAL_FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2578 | else |
| 2579 | { |
| 2580 | // "var" can be script-local even without using "s:" if it |
| 2581 | // already exists. |
| 2582 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2583 | == SCRIPT_VERSION_VIM9 |
| 2584 | || lookup_script(*arg, len) == OK) |
| 2585 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2586 | FALSE); |
| 2587 | |
| 2588 | // When the name starts with an uppercase letter or "x:" it |
| 2589 | // can be a user defined function. |
| 2590 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2591 | res = generate_funcref(cctx, name); |
| 2592 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | if (gen_load) |
| 2596 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2597 | if (gen_load_outer) |
| 2598 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | *arg = end; |
| 2602 | |
| 2603 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2604 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2605 | semsg(_(e_var_notfound), name); |
| 2606 | vim_free(name); |
| 2607 | return res; |
| 2608 | } |
| 2609 | |
| 2610 | /* |
| 2611 | * Compile the argument expressions. |
| 2612 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2613 | */ |
| 2614 | static int |
| 2615 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2616 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2617 | char_u *p = *arg; |
| 2618 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2619 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2620 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2621 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2622 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2623 | { |
| 2624 | p = next_line_from_context(cctx); |
| 2625 | if (p == NULL) |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2626 | goto failret; |
| 2627 | whitep = (char_u *)" "; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2628 | p = skipwhite(p); |
| 2629 | } |
| 2630 | if (*p == ')') |
| 2631 | { |
| 2632 | *arg = p + 1; |
| 2633 | return OK; |
| 2634 | } |
| 2635 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2636 | if (compile_expr1(&p, cctx) == FAIL) |
| 2637 | return FAIL; |
| 2638 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2639 | |
| 2640 | if (*p != ',' && *skipwhite(p) == ',') |
| 2641 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2642 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2643 | p = skipwhite(p); |
| 2644 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2645 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2646 | { |
| 2647 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2648 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2649 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2650 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2651 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2652 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2653 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2654 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2655 | emsg(_(e_missing_close)); |
| 2656 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /* |
| 2660 | * Compile a function call: name(arg1, arg2) |
| 2661 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2662 | * "argcount_init" is 1 for "value->method()" |
| 2663 | * Instructions: |
| 2664 | * EVAL arg1 |
| 2665 | * EVAL arg2 |
| 2666 | * BCALL / DCALL / UCALL |
| 2667 | */ |
| 2668 | static int |
| 2669 | compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init) |
| 2670 | { |
| 2671 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2672 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2673 | int argcount = argcount_init; |
| 2674 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2675 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2676 | char_u *tofree = NULL; |
| 2677 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2678 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2679 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2680 | |
| 2681 | if (varlen >= sizeof(namebuf)) |
| 2682 | { |
| 2683 | semsg(_("E1011: name too long: %s"), name); |
| 2684 | return FAIL; |
| 2685 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2686 | vim_strncpy(namebuf, *arg, varlen); |
| 2687 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2688 | |
| 2689 | *arg = skipwhite(*arg + varlen + 1); |
| 2690 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2691 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2692 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2693 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2694 | { |
| 2695 | int idx; |
| 2696 | |
| 2697 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2698 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2699 | if (idx >= 0) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2700 | res = generate_BCALL(cctx, idx, argcount); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2701 | else |
| 2702 | semsg(_(e_unknownfunc), namebuf); |
| 2703 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2706 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2707 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2708 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2709 | { |
| 2710 | res = generate_CALL(cctx, ufunc, argcount); |
| 2711 | goto theend; |
| 2712 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2713 | |
| 2714 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2715 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2716 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2717 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 2718 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2719 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2720 | garray_T *stack = &cctx->ctx_type_stack; |
| 2721 | type_T *type; |
| 2722 | |
| 2723 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2724 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2725 | goto theend; |
| 2726 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2727 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2728 | // 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] | 2729 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2730 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 2731 | res = generate_UCALL(cctx, name, argcount); |
| 2732 | else |
| 2733 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2734 | |
| 2735 | theend: |
| 2736 | vim_free(tofree); |
| 2737 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2741 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2742 | |
| 2743 | /* |
| 2744 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2745 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2746 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2747 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2748 | * valid name. |
| 2749 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2750 | static char_u * |
| 2751 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2752 | { |
| 2753 | char_u *p; |
| 2754 | |
| 2755 | // Quick check for valid starting character. |
| 2756 | if (!eval_isnamec1(*arg)) |
| 2757 | return arg; |
| 2758 | |
| 2759 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2760 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2761 | // and can be used in slice "[n:]". |
| 2762 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2763 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2764 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2765 | break; |
| 2766 | return p; |
| 2767 | } |
| 2768 | |
| 2769 | /* |
| 2770 | * Like to_name_end() but also skip over a list or dict constant. |
| 2771 | */ |
| 2772 | char_u * |
| 2773 | to_name_const_end(char_u *arg) |
| 2774 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2775 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2776 | typval_T rettv; |
| 2777 | |
| 2778 | if (p == arg && *arg == '[') |
| 2779 | { |
| 2780 | |
| 2781 | // Can be "[1, 2, 3]->Func()". |
| 2782 | if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL) |
| 2783 | p = arg; |
| 2784 | } |
| 2785 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2786 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2787 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2788 | ++p; |
| 2789 | if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL) |
| 2790 | p = arg; |
| 2791 | } |
| 2792 | else if (p == arg && *arg == '{') |
| 2793 | { |
| 2794 | int ret = get_lambda_tv(&p, &rettv, FALSE); |
| 2795 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2796 | // Can be "{x -> ret}()". |
| 2797 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2798 | if (ret == NOTDONE) |
| 2799 | ret = eval_dict(&p, &rettv, FALSE, FALSE); |
| 2800 | if (ret != OK) |
| 2801 | p = arg; |
| 2802 | } |
| 2803 | |
| 2804 | return p; |
| 2805 | } |
| 2806 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2807 | /* |
| 2808 | * parse a list: [expr, expr] |
| 2809 | * "*arg" points to the '['. |
| 2810 | */ |
| 2811 | static int |
| 2812 | compile_list(char_u **arg, cctx_T *cctx) |
| 2813 | { |
| 2814 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2815 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2816 | int count = 0; |
| 2817 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2818 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2819 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2820 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2821 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2822 | p = next_line_from_context(cctx); |
| 2823 | if (p == NULL) |
| 2824 | { |
| 2825 | semsg(_(e_list_end), *arg); |
| 2826 | return FAIL; |
| 2827 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2828 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2829 | p = skipwhite(p); |
| 2830 | } |
| 2831 | if (*p == ']') |
| 2832 | { |
| 2833 | ++p; |
| 2834 | // Allow for following comment, after at least one space. |
| 2835 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '"') |
| 2836 | p += STRLEN(p); |
| 2837 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2838 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2839 | if (compile_expr1(&p, cctx) == FAIL) |
| 2840 | break; |
| 2841 | ++count; |
| 2842 | if (*p == ',') |
| 2843 | ++p; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2844 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2845 | p = skipwhite(p); |
| 2846 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2847 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2848 | |
| 2849 | generate_NEWLIST(cctx, count); |
| 2850 | return OK; |
| 2851 | } |
| 2852 | |
| 2853 | /* |
| 2854 | * parse a lambda: {arg, arg -> expr} |
| 2855 | * "*arg" points to the '{'. |
| 2856 | */ |
| 2857 | static int |
| 2858 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2859 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2860 | typval_T rettv; |
| 2861 | ufunc_T *ufunc; |
| 2862 | |
| 2863 | // Get the funcref in "rettv". |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2864 | if (get_lambda_tv(arg, &rettv, TRUE) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2865 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2866 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2867 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2868 | ++ufunc->uf_refcount; |
| 2869 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2870 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2871 | |
| 2872 | // The function will have one line: "return {expr}". |
| 2873 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2874 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2875 | |
| 2876 | if (ufunc->uf_dfunc_idx >= 0) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 2877 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2878 | return FAIL; |
| 2879 | } |
| 2880 | |
| 2881 | /* |
| 2882 | * Compile a lamda call: expr->{lambda}(args) |
| 2883 | * "arg" points to the "{". |
| 2884 | */ |
| 2885 | static int |
| 2886 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2887 | { |
| 2888 | ufunc_T *ufunc; |
| 2889 | typval_T rettv; |
| 2890 | int argcount = 1; |
| 2891 | int ret = FAIL; |
| 2892 | |
| 2893 | // Get the funcref in "rettv". |
| 2894 | if (get_lambda_tv(arg, &rettv, TRUE) == FAIL) |
| 2895 | return FAIL; |
| 2896 | |
| 2897 | if (**arg != '(') |
| 2898 | { |
| 2899 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2900 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2901 | else |
| 2902 | semsg(_(e_missing_paren), "lambda"); |
| 2903 | clear_tv(&rettv); |
| 2904 | return FAIL; |
| 2905 | } |
| 2906 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2907 | ufunc = rettv.vval.v_partial->pt_func; |
| 2908 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2909 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2910 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2911 | |
| 2912 | // The function will have one line: "return {expr}". |
| 2913 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2914 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2915 | |
| 2916 | // compile the arguments |
| 2917 | *arg = skipwhite(*arg + 1); |
| 2918 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2919 | // call the compiled function |
| 2920 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2921 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2922 | return ret; |
| 2923 | } |
| 2924 | |
| 2925 | /* |
| 2926 | * parse a dict: {'key': val} or #{key: val} |
| 2927 | * "*arg" points to the '{'. |
| 2928 | */ |
| 2929 | static int |
| 2930 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2931 | { |
| 2932 | garray_T *instr = &cctx->ctx_instr; |
| 2933 | int count = 0; |
| 2934 | dict_T *d = dict_alloc(); |
| 2935 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2936 | char_u *whitep = *arg; |
| 2937 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | |
| 2939 | if (d == NULL) |
| 2940 | return FAIL; |
| 2941 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2942 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2943 | { |
| 2944 | char_u *key = NULL; |
| 2945 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2946 | while (**arg == NUL || (literal && **arg == '"') |
| 2947 | || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2948 | { |
| 2949 | *arg = next_line_from_context(cctx); |
| 2950 | if (*arg == NULL) |
| 2951 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2952 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2953 | *arg = skipwhite(*arg); |
| 2954 | } |
| 2955 | |
| 2956 | if (**arg == '}') |
| 2957 | break; |
| 2958 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2959 | if (literal) |
| 2960 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2961 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2962 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2963 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2964 | { |
| 2965 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 2966 | return FAIL; |
| 2967 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2968 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2969 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2970 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2971 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2972 | } |
| 2973 | else |
| 2974 | { |
| 2975 | isn_T *isn; |
| 2976 | |
| 2977 | if (compile_expr1(arg, cctx) == FAIL) |
| 2978 | return FAIL; |
| 2979 | // TODO: check type is string |
| 2980 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2981 | if (isn->isn_type == ISN_PUSHS) |
| 2982 | key = isn->isn_arg.string; |
| 2983 | } |
| 2984 | |
| 2985 | // Check for duplicate keys, if using string keys. |
| 2986 | if (key != NULL) |
| 2987 | { |
| 2988 | item = dict_find(d, key, -1); |
| 2989 | if (item != NULL) |
| 2990 | { |
| 2991 | semsg(_(e_duplicate_key), key); |
| 2992 | goto failret; |
| 2993 | } |
| 2994 | item = dictitem_alloc(key); |
| 2995 | if (item != NULL) |
| 2996 | { |
| 2997 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2998 | item->di_tv.v_lock = 0; |
| 2999 | if (dict_add(d, item) == FAIL) |
| 3000 | dictitem_free(item); |
| 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | *arg = skipwhite(*arg); |
| 3005 | if (**arg != ':') |
| 3006 | { |
| 3007 | semsg(_(e_missing_dict_colon), *arg); |
| 3008 | return FAIL; |
| 3009 | } |
| 3010 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3011 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3012 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3013 | while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3014 | { |
| 3015 | *arg = next_line_from_context(cctx); |
| 3016 | if (*arg == NULL) |
| 3017 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3018 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3019 | *arg = skipwhite(*arg); |
| 3020 | } |
| 3021 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3022 | if (compile_expr1(arg, cctx) == FAIL) |
| 3023 | return FAIL; |
| 3024 | ++count; |
| 3025 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3026 | whitep = *arg; |
| 3027 | p = skipwhite(*arg); |
| 3028 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3029 | { |
| 3030 | *arg = next_line_from_context(cctx); |
| 3031 | if (*arg == NULL) |
| 3032 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3033 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3034 | *arg = skipwhite(*arg); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3035 | p = *arg; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3036 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | if (**arg == '}') |
| 3038 | break; |
| 3039 | if (**arg != ',') |
| 3040 | { |
| 3041 | semsg(_(e_missing_dict_comma), *arg); |
| 3042 | goto failret; |
| 3043 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3044 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3045 | *arg = skipwhite(*arg + 1); |
| 3046 | } |
| 3047 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3048 | *arg = *arg + 1; |
| 3049 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3050 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3051 | p = skipwhite(*arg); |
| 3052 | if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3053 | *arg += STRLEN(*arg); |
| 3054 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3055 | dict_unref(d); |
| 3056 | return generate_NEWDICT(cctx, count); |
| 3057 | |
| 3058 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3059 | if (*arg == NULL) |
| 3060 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3061 | dict_unref(d); |
| 3062 | return FAIL; |
| 3063 | } |
| 3064 | |
| 3065 | /* |
| 3066 | * Compile "&option". |
| 3067 | */ |
| 3068 | static int |
| 3069 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3070 | { |
| 3071 | typval_T rettv; |
| 3072 | char_u *start = *arg; |
| 3073 | int ret; |
| 3074 | |
| 3075 | // parse the option and get the current value to get the type. |
| 3076 | rettv.v_type = VAR_UNKNOWN; |
| 3077 | ret = get_option_tv(arg, &rettv, TRUE); |
| 3078 | if (ret == OK) |
| 3079 | { |
| 3080 | // include the '&' in the name, get_option_tv() expects it. |
| 3081 | char_u *name = vim_strnsave(start, *arg - start); |
| 3082 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3083 | |
| 3084 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3085 | vim_free(name); |
| 3086 | } |
| 3087 | clear_tv(&rettv); |
| 3088 | |
| 3089 | return ret; |
| 3090 | } |
| 3091 | |
| 3092 | /* |
| 3093 | * Compile "$VAR". |
| 3094 | */ |
| 3095 | static int |
| 3096 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3097 | { |
| 3098 | char_u *start = *arg; |
| 3099 | int len; |
| 3100 | int ret; |
| 3101 | char_u *name; |
| 3102 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3103 | ++*arg; |
| 3104 | len = get_env_len(arg); |
| 3105 | if (len == 0) |
| 3106 | { |
| 3107 | semsg(_(e_syntax_at), start - 1); |
| 3108 | return FAIL; |
| 3109 | } |
| 3110 | |
| 3111 | // include the '$' in the name, get_env_tv() expects it. |
| 3112 | name = vim_strnsave(start, len + 1); |
| 3113 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3114 | vim_free(name); |
| 3115 | return ret; |
| 3116 | } |
| 3117 | |
| 3118 | /* |
| 3119 | * Compile "@r". |
| 3120 | */ |
| 3121 | static int |
| 3122 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3123 | { |
| 3124 | int ret; |
| 3125 | |
| 3126 | ++*arg; |
| 3127 | if (**arg == NUL) |
| 3128 | { |
| 3129 | semsg(_(e_syntax_at), *arg - 1); |
| 3130 | return FAIL; |
| 3131 | } |
| 3132 | if (!valid_yank_reg(**arg, TRUE)) |
| 3133 | { |
| 3134 | emsg_invreg(**arg); |
| 3135 | return FAIL; |
| 3136 | } |
| 3137 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3138 | ++*arg; |
| 3139 | return ret; |
| 3140 | } |
| 3141 | |
| 3142 | /* |
| 3143 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3144 | */ |
| 3145 | static int |
| 3146 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3147 | { |
| 3148 | char_u *p = end; |
| 3149 | |
| 3150 | // this works from end to start |
| 3151 | while (p > start) |
| 3152 | { |
| 3153 | --p; |
| 3154 | if (*p == '-' || *p == '+') |
| 3155 | { |
| 3156 | // only '-' has an effect, for '+' we only check the type |
| 3157 | #ifdef FEAT_FLOAT |
| 3158 | if (rettv->v_type == VAR_FLOAT) |
| 3159 | { |
| 3160 | if (*p == '-') |
| 3161 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3162 | } |
| 3163 | else |
| 3164 | #endif |
| 3165 | { |
| 3166 | varnumber_T val; |
| 3167 | int error = FALSE; |
| 3168 | |
| 3169 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3170 | // here |
| 3171 | if (check_not_string(rettv) == FAIL) |
| 3172 | return FAIL; |
| 3173 | val = tv_get_number_chk(rettv, &error); |
| 3174 | clear_tv(rettv); |
| 3175 | if (error) |
| 3176 | return FAIL; |
| 3177 | if (*p == '-') |
| 3178 | val = -val; |
| 3179 | rettv->v_type = VAR_NUMBER; |
| 3180 | rettv->vval.v_number = val; |
| 3181 | } |
| 3182 | } |
| 3183 | else |
| 3184 | { |
| 3185 | int v = tv2bool(rettv); |
| 3186 | |
| 3187 | // '!' is permissive in the type. |
| 3188 | clear_tv(rettv); |
| 3189 | rettv->v_type = VAR_BOOL; |
| 3190 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3191 | } |
| 3192 | } |
| 3193 | return OK; |
| 3194 | } |
| 3195 | |
| 3196 | /* |
| 3197 | * Recognize v: variables that are constants and set "rettv". |
| 3198 | */ |
| 3199 | static void |
| 3200 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3201 | { |
| 3202 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3203 | { |
| 3204 | rettv->v_type = VAR_BOOL; |
| 3205 | rettv->vval.v_number = VVAL_TRUE; |
| 3206 | *arg += 6; |
| 3207 | } |
| 3208 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3209 | { |
| 3210 | rettv->v_type = VAR_BOOL; |
| 3211 | rettv->vval.v_number = VVAL_FALSE; |
| 3212 | *arg += 7; |
| 3213 | } |
| 3214 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3215 | { |
| 3216 | rettv->v_type = VAR_SPECIAL; |
| 3217 | rettv->vval.v_number = VVAL_NULL; |
| 3218 | *arg += 6; |
| 3219 | } |
| 3220 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3221 | { |
| 3222 | rettv->v_type = VAR_SPECIAL; |
| 3223 | rettv->vval.v_number = VVAL_NONE; |
| 3224 | *arg += 6; |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | /* |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3229 | * Evaluate an expression that is a constant: |
| 3230 | * has(arg) |
| 3231 | * |
| 3232 | * Also handle: |
| 3233 | * ! in front logical NOT |
| 3234 | * |
| 3235 | * Return FAIL if the expression is not a constant. |
| 3236 | */ |
| 3237 | static int |
| 3238 | evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv) |
| 3239 | { |
| 3240 | typval_T argvars[2]; |
| 3241 | char_u *start_leader, *end_leader; |
| 3242 | int has_call = FALSE; |
| 3243 | |
| 3244 | /* |
| 3245 | * Skip '!' characters. They are handled later. |
| 3246 | * TODO: '-' and '+' characters |
| 3247 | */ |
| 3248 | start_leader = *arg; |
| 3249 | while (**arg == '!') |
| 3250 | *arg = skipwhite(*arg + 1); |
| 3251 | end_leader = *arg; |
| 3252 | |
| 3253 | /* |
| 3254 | * Recognize only a few types of constants for now. |
| 3255 | */ |
| 3256 | if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4])) |
| 3257 | { |
| 3258 | tv->v_type = VAR_BOOL; |
| 3259 | tv->vval.v_number = VVAL_TRUE; |
| 3260 | *arg += 4; |
| 3261 | return OK; |
| 3262 | } |
| 3263 | if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5])) |
| 3264 | { |
| 3265 | tv->v_type = VAR_BOOL; |
| 3266 | tv->vval.v_number = VVAL_FALSE; |
| 3267 | *arg += 5; |
| 3268 | return OK; |
| 3269 | } |
| 3270 | |
| 3271 | if (STRNCMP("has(", *arg, 4) == 0) |
| 3272 | { |
| 3273 | has_call = TRUE; |
| 3274 | *arg = skipwhite(*arg + 4); |
| 3275 | } |
| 3276 | |
| 3277 | if (**arg == '"') |
| 3278 | { |
| 3279 | if (get_string_tv(arg, tv, TRUE) == FAIL) |
| 3280 | return FAIL; |
| 3281 | } |
| 3282 | else if (**arg == '\'') |
| 3283 | { |
| 3284 | if (get_lit_string_tv(arg, tv, TRUE) == FAIL) |
| 3285 | return FAIL; |
| 3286 | } |
| 3287 | else |
| 3288 | return FAIL; |
| 3289 | |
| 3290 | if (has_call) |
| 3291 | { |
| 3292 | *arg = skipwhite(*arg); |
| 3293 | if (**arg != ')') |
| 3294 | return FAIL; |
| 3295 | *arg = *arg + 1; |
| 3296 | |
| 3297 | argvars[0] = *tv; |
| 3298 | argvars[1].v_type = VAR_UNKNOWN; |
| 3299 | tv->v_type = VAR_NUMBER; |
| 3300 | tv->vval.v_number = 0; |
| 3301 | f_has(argvars, tv); |
| 3302 | clear_tv(&argvars[0]); |
| 3303 | |
| 3304 | while (start_leader < end_leader) |
| 3305 | { |
| 3306 | if (*start_leader == '!') |
| 3307 | tv->vval.v_number = !tv->vval.v_number; |
| 3308 | ++start_leader; |
| 3309 | } |
| 3310 | } |
| 3311 | else if (end_leader > start_leader) |
| 3312 | { |
| 3313 | clear_tv(tv); |
| 3314 | return FAIL; |
| 3315 | } |
| 3316 | |
| 3317 | return OK; |
| 3318 | } |
| 3319 | |
| 3320 | /* |
| 3321 | * * number multiplication |
| 3322 | * / number division |
| 3323 | * % number modulo |
| 3324 | */ |
| 3325 | static int |
| 3326 | evaluate_const_expr6(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 3327 | { |
| 3328 | char_u *op; |
| 3329 | |
| 3330 | // get the first variable |
| 3331 | if (evaluate_const_expr7(arg, cctx, tv) == FAIL) |
| 3332 | return FAIL; |
| 3333 | |
| 3334 | /* |
| 3335 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3336 | */ |
| 3337 | for (;;) |
| 3338 | { |
| 3339 | op = skipwhite(*arg); |
| 3340 | if (*op != '*' && *op != '/' && *op != '%') |
| 3341 | break; |
| 3342 | // TODO: not implemented yet. |
| 3343 | clear_tv(tv); |
| 3344 | return FAIL; |
| 3345 | } |
| 3346 | return OK; |
| 3347 | } |
| 3348 | |
| 3349 | /* |
| 3350 | * + number addition |
| 3351 | * - number subtraction |
| 3352 | * .. string concatenation |
| 3353 | */ |
| 3354 | static int |
| 3355 | evaluate_const_expr5(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 3356 | { |
| 3357 | char_u *op; |
| 3358 | int oplen; |
| 3359 | |
| 3360 | // get the first variable |
| 3361 | if (evaluate_const_expr6(arg, cctx, tv) == FAIL) |
| 3362 | return FAIL; |
| 3363 | |
| 3364 | /* |
| 3365 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3366 | */ |
| 3367 | for (;;) |
| 3368 | { |
| 3369 | op = skipwhite(*arg); |
| 3370 | if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.'))) |
| 3371 | break; |
| 3372 | oplen = (*op == '.' ? 2 : 1); |
| 3373 | |
| 3374 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
| 3375 | { |
| 3376 | clear_tv(tv); |
| 3377 | return FAIL; |
| 3378 | } |
| 3379 | |
| 3380 | if (*op == '.' && tv->v_type == VAR_STRING) |
| 3381 | { |
| 3382 | typval_T tv2; |
| 3383 | size_t len1; |
| 3384 | char_u *s1, *s2; |
| 3385 | |
| 3386 | tv2.v_type = VAR_UNKNOWN; |
| 3387 | *arg = skipwhite(op + oplen); |
| 3388 | |
| 3389 | // TODO: what if we fail??? |
| 3390 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
| 3391 | return FAIL; |
| 3392 | |
| 3393 | // get the second variable |
| 3394 | if (evaluate_const_expr6(arg, cctx, &tv2) == FAIL) |
| 3395 | { |
| 3396 | clear_tv(tv); |
| 3397 | return FAIL; |
| 3398 | } |
| 3399 | if (tv2.v_type != VAR_STRING) |
| 3400 | { |
| 3401 | clear_tv(tv); |
| 3402 | clear_tv(&tv2); |
| 3403 | return FAIL; |
| 3404 | } |
| 3405 | s1 = tv->vval.v_string; |
| 3406 | len1 = STRLEN(s1); |
| 3407 | s2 = tv2.vval.v_string; |
| 3408 | tv->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3409 | if (tv->vval.v_string == NULL) |
| 3410 | { |
| 3411 | vim_free(s1); |
| 3412 | vim_free(s2); |
| 3413 | return FAIL; |
| 3414 | } |
| 3415 | mch_memmove(tv->vval.v_string, s1, len1); |
| 3416 | STRCPY(tv->vval.v_string + len1, s2); |
| 3417 | continue; |
| 3418 | } |
| 3419 | |
| 3420 | // TODO: Not implemented yet. |
| 3421 | clear_tv(tv); |
| 3422 | return FAIL; |
| 3423 | } |
| 3424 | return OK; |
| 3425 | } |
| 3426 | |
| 3427 | static exptype_T |
| 3428 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3429 | { |
| 3430 | exptype_T type = EXPR_UNKNOWN; |
| 3431 | int i; |
| 3432 | |
| 3433 | switch (p[0]) |
| 3434 | { |
| 3435 | case '=': if (p[1] == '=') |
| 3436 | type = EXPR_EQUAL; |
| 3437 | else if (p[1] == '~') |
| 3438 | type = EXPR_MATCH; |
| 3439 | break; |
| 3440 | case '!': if (p[1] == '=') |
| 3441 | type = EXPR_NEQUAL; |
| 3442 | else if (p[1] == '~') |
| 3443 | type = EXPR_NOMATCH; |
| 3444 | break; |
| 3445 | case '>': if (p[1] != '=') |
| 3446 | { |
| 3447 | type = EXPR_GREATER; |
| 3448 | *len = 1; |
| 3449 | } |
| 3450 | else |
| 3451 | type = EXPR_GEQUAL; |
| 3452 | break; |
| 3453 | case '<': if (p[1] != '=') |
| 3454 | { |
| 3455 | type = EXPR_SMALLER; |
| 3456 | *len = 1; |
| 3457 | } |
| 3458 | else |
| 3459 | type = EXPR_SEQUAL; |
| 3460 | break; |
| 3461 | case 'i': if (p[1] == 's') |
| 3462 | { |
| 3463 | // "is" and "isnot"; but not a prefix of a name |
| 3464 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3465 | *len = 5; |
| 3466 | i = p[*len]; |
| 3467 | if (!isalnum(i) && i != '_') |
| 3468 | { |
| 3469 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3470 | *type_is = TRUE; |
| 3471 | } |
| 3472 | } |
| 3473 | break; |
| 3474 | } |
| 3475 | return type; |
| 3476 | } |
| 3477 | |
| 3478 | /* |
| 3479 | * Only comparing strings is supported right now. |
| 3480 | * expr5a == expr5b |
| 3481 | */ |
| 3482 | static int |
| 3483 | evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv) |
| 3484 | { |
| 3485 | exptype_T type = EXPR_UNKNOWN; |
| 3486 | char_u *p; |
| 3487 | int len = 2; |
| 3488 | int type_is = FALSE; |
| 3489 | |
| 3490 | // get the first variable |
| 3491 | if (evaluate_const_expr5(arg, cctx, tv) == FAIL) |
| 3492 | return FAIL; |
| 3493 | |
| 3494 | p = skipwhite(*arg); |
| 3495 | type = get_compare_type(p, &len, &type_is); |
| 3496 | |
| 3497 | /* |
| 3498 | * If there is a comparative operator, use it. |
| 3499 | */ |
| 3500 | if (type != EXPR_UNKNOWN) |
| 3501 | { |
| 3502 | typval_T tv2; |
| 3503 | char_u *s1, *s2; |
| 3504 | char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; |
| 3505 | int n; |
| 3506 | |
| 3507 | // TODO: Only string == string is supported now |
| 3508 | if (tv->v_type != VAR_STRING) |
| 3509 | return FAIL; |
| 3510 | if (type != EXPR_EQUAL) |
| 3511 | return FAIL; |
| 3512 | |
| 3513 | // get the second variable |
| 3514 | init_tv(&tv2); |
| 3515 | *arg = skipwhite(p + len); |
| 3516 | if (evaluate_const_expr5(arg, cctx, &tv2) == FAIL |
| 3517 | || tv2.v_type != VAR_STRING) |
| 3518 | { |
| 3519 | clear_tv(&tv2); |
| 3520 | return FAIL; |
| 3521 | } |
| 3522 | s1 = tv_get_string_buf(tv, buf1); |
| 3523 | s2 = tv_get_string_buf(&tv2, buf2); |
| 3524 | n = STRCMP(s1, s2); |
| 3525 | clear_tv(tv); |
| 3526 | clear_tv(&tv2); |
| 3527 | tv->v_type = VAR_BOOL; |
| 3528 | tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE; |
| 3529 | } |
| 3530 | |
| 3531 | return OK; |
| 3532 | } |
| 3533 | |
| 3534 | static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv); |
| 3535 | |
| 3536 | /* |
| 3537 | * Compile constant || or &&. |
| 3538 | */ |
| 3539 | static int |
| 3540 | evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv) |
| 3541 | { |
| 3542 | char_u *p = skipwhite(*arg); |
| 3543 | int opchar = *op; |
| 3544 | |
| 3545 | if (p[0] == opchar && p[1] == opchar) |
| 3546 | { |
| 3547 | int val = tv2bool(tv); |
| 3548 | |
| 3549 | /* |
| 3550 | * Repeat until there is no following "||" or "&&" |
| 3551 | */ |
| 3552 | while (p[0] == opchar && p[1] == opchar) |
| 3553 | { |
| 3554 | typval_T tv2; |
| 3555 | |
| 3556 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2])) |
| 3557 | return FAIL; |
| 3558 | |
| 3559 | // eval the next expression |
| 3560 | *arg = skipwhite(p + 2); |
| 3561 | tv2.v_type = VAR_UNKNOWN; |
| 3562 | tv2.v_lock = 0; |
| 3563 | if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2) |
| 3564 | : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL) |
| 3565 | { |
| 3566 | clear_tv(&tv2); |
| 3567 | return FAIL; |
| 3568 | } |
| 3569 | if ((opchar == '&') == val) |
| 3570 | { |
| 3571 | // false || tv2 or true && tv2: use tv2 |
| 3572 | clear_tv(tv); |
| 3573 | *tv = tv2; |
| 3574 | val = tv2bool(tv); |
| 3575 | } |
| 3576 | else |
| 3577 | clear_tv(&tv2); |
| 3578 | p = skipwhite(*arg); |
| 3579 | } |
| 3580 | } |
| 3581 | |
| 3582 | return OK; |
| 3583 | } |
| 3584 | |
| 3585 | /* |
| 3586 | * Evaluate an expression that is a constant: expr4 && expr4 && expr4 |
| 3587 | * Return FAIL if the expression is not a constant. |
| 3588 | */ |
| 3589 | static int |
| 3590 | evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 3591 | { |
| 3592 | // evaluate the first expression |
| 3593 | if (evaluate_const_expr4(arg, cctx, tv) == FAIL) |
| 3594 | return FAIL; |
| 3595 | |
| 3596 | // || and && work almost the same |
| 3597 | return evaluate_const_and_or(arg, cctx, "&&", tv); |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | * Evaluate an expression that is a constant: expr3 || expr3 || expr3 |
| 3602 | * Return FAIL if the expression is not a constant. |
| 3603 | */ |
| 3604 | static int |
| 3605 | evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 3606 | { |
| 3607 | // evaluate the first expression |
| 3608 | if (evaluate_const_expr3(arg, cctx, tv) == FAIL) |
| 3609 | return FAIL; |
| 3610 | |
| 3611 | // || and && work almost the same |
| 3612 | return evaluate_const_and_or(arg, cctx, "||", tv); |
| 3613 | } |
| 3614 | |
| 3615 | /* |
| 3616 | * Evaluate an expression that is a constant: expr2 ? expr1 : expr1 |
| 3617 | * E.g. for "has('feature')". |
| 3618 | * This does not produce error messages. "tv" should be cleared afterwards. |
| 3619 | * Return FAIL if the expression is not a constant. |
| 3620 | */ |
| 3621 | static int |
| 3622 | evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 3623 | { |
| 3624 | char_u *p; |
| 3625 | |
| 3626 | // evaluate the first expression |
| 3627 | if (evaluate_const_expr2(arg, cctx, tv) == FAIL) |
| 3628 | return FAIL; |
| 3629 | |
| 3630 | p = skipwhite(*arg); |
| 3631 | if (*p == '?') |
| 3632 | { |
| 3633 | int val = tv2bool(tv); |
| 3634 | typval_T tv2; |
| 3635 | |
| 3636 | // require space before and after the ? |
| 3637 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 3638 | return FAIL; |
| 3639 | |
| 3640 | // evaluate the second expression; any type is accepted |
| 3641 | clear_tv(tv); |
| 3642 | *arg = skipwhite(p + 1); |
| 3643 | if (evaluate_const_expr1(arg, cctx, tv) == FAIL) |
| 3644 | return FAIL; |
| 3645 | |
| 3646 | // Check for the ":". |
| 3647 | p = skipwhite(*arg); |
| 3648 | if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 3649 | return FAIL; |
| 3650 | |
| 3651 | // evaluate the third expression |
| 3652 | *arg = skipwhite(p + 1); |
| 3653 | tv2.v_type = VAR_UNKNOWN; |
| 3654 | if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL) |
| 3655 | { |
| 3656 | clear_tv(&tv2); |
| 3657 | return FAIL; |
| 3658 | } |
| 3659 | if (val) |
| 3660 | { |
| 3661 | // use the expr after "?" |
| 3662 | clear_tv(&tv2); |
| 3663 | } |
| 3664 | else |
| 3665 | { |
| 3666 | // use the expr after ":" |
| 3667 | clear_tv(tv); |
| 3668 | *tv = tv2; |
| 3669 | } |
| 3670 | } |
| 3671 | return OK; |
| 3672 | } |
| 3673 | |
| 3674 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3675 | * Compile code to apply '-', '+' and '!'. |
| 3676 | */ |
| 3677 | static int |
| 3678 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3679 | { |
| 3680 | char_u *p = end; |
| 3681 | |
| 3682 | // this works from end to start |
| 3683 | while (p > start) |
| 3684 | { |
| 3685 | --p; |
| 3686 | if (*p == '-' || *p == '+') |
| 3687 | { |
| 3688 | int negate = *p == '-'; |
| 3689 | isn_T *isn; |
| 3690 | |
| 3691 | // TODO: check type |
| 3692 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3693 | { |
| 3694 | --p; |
| 3695 | if (*p == '-') |
| 3696 | negate = !negate; |
| 3697 | } |
| 3698 | // only '-' has an effect, for '+' we only check the type |
| 3699 | if (negate) |
| 3700 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3701 | else |
| 3702 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3703 | if (isn == NULL) |
| 3704 | return FAIL; |
| 3705 | } |
| 3706 | else |
| 3707 | { |
| 3708 | int invert = TRUE; |
| 3709 | |
| 3710 | while (p > start && p[-1] == '!') |
| 3711 | { |
| 3712 | --p; |
| 3713 | invert = !invert; |
| 3714 | } |
| 3715 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3716 | return FAIL; |
| 3717 | } |
| 3718 | } |
| 3719 | return OK; |
| 3720 | } |
| 3721 | |
| 3722 | /* |
| 3723 | * Compile whatever comes after "name" or "name()". |
| 3724 | */ |
| 3725 | static int |
| 3726 | compile_subscript( |
| 3727 | char_u **arg, |
| 3728 | cctx_T *cctx, |
| 3729 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3730 | char_u *end_leader, |
| 3731 | typval_T *bef1_tv, |
| 3732 | typval_T *bef2_tv, |
| 3733 | typval_T *new_tv) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3734 | { |
| 3735 | for (;;) |
| 3736 | { |
| 3737 | if (**arg == '(') |
| 3738 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3739 | garray_T *stack = &cctx->ctx_type_stack; |
| 3740 | type_T *type; |
| 3741 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3742 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3743 | if (generate_tv_PUSH(cctx, bef1_tv) == FAIL |
| 3744 | || generate_tv_PUSH(cctx, bef2_tv) == FAIL |
| 3745 | || generate_tv_PUSH(cctx, new_tv) == FAIL) |
| 3746 | return FAIL; |
| 3747 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3748 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3749 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3750 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3751 | *arg = skipwhite(*arg + 1); |
| 3752 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3753 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3754 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3755 | return FAIL; |
| 3756 | } |
| 3757 | else if (**arg == '-' && (*arg)[1] == '>') |
| 3758 | { |
| 3759 | char_u *p; |
| 3760 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3761 | if (generate_tv_PUSH(cctx, bef1_tv) == FAIL |
| 3762 | || generate_tv_PUSH(cctx, bef2_tv) == FAIL |
| 3763 | || generate_tv_PUSH(cctx, new_tv) == FAIL) |
| 3764 | return FAIL; |
| 3765 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3766 | // something->method() |
| 3767 | // Apply the '!', '-' and '+' first: |
| 3768 | // -1.0->func() works like (-1.0)->func() |
| 3769 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3770 | return FAIL; |
| 3771 | *start_leader = end_leader; // don't apply again later |
| 3772 | |
| 3773 | *arg = skipwhite(*arg + 2); |
| 3774 | if (**arg == '{') |
| 3775 | { |
| 3776 | // lambda call: list->{lambda} |
| 3777 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3778 | return FAIL; |
| 3779 | } |
| 3780 | else |
| 3781 | { |
| 3782 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3783 | p = *arg; |
| 3784 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3785 | p += 2; |
| 3786 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3787 | ; |
| 3788 | if (*p != '(') |
| 3789 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3790 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3791 | return FAIL; |
| 3792 | } |
| 3793 | // TODO: base value may not be the first argument |
| 3794 | if (compile_call(arg, p - *arg, cctx, 1) == FAIL) |
| 3795 | return FAIL; |
| 3796 | } |
| 3797 | } |
| 3798 | else if (**arg == '[') |
| 3799 | { |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3800 | garray_T *stack; |
| 3801 | type_T **typep; |
| 3802 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3803 | if (generate_tv_PUSH(cctx, bef1_tv) == FAIL |
| 3804 | || generate_tv_PUSH(cctx, bef2_tv) == FAIL |
| 3805 | || generate_tv_PUSH(cctx, new_tv) == FAIL) |
| 3806 | return FAIL; |
| 3807 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3808 | // list index: list[123] |
| 3809 | // TODO: more arguments |
| 3810 | // TODO: dict member dict['name'] |
| 3811 | *arg = skipwhite(*arg + 1); |
| 3812 | if (compile_expr1(arg, cctx) == FAIL) |
| 3813 | return FAIL; |
| 3814 | |
| 3815 | if (**arg != ']') |
| 3816 | { |
| 3817 | emsg(_(e_missbrac)); |
| 3818 | return FAIL; |
| 3819 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3820 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3821 | |
| 3822 | if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) |
| 3823 | return FAIL; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3824 | stack = &cctx->ctx_type_stack; |
| 3825 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 3826 | if ((*typep)->tt_type != VAR_LIST && *typep != &t_any) |
| 3827 | { |
| 3828 | emsg(_(e_listreq)); |
| 3829 | return FAIL; |
| 3830 | } |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 3831 | if ((*typep)->tt_type == VAR_LIST) |
| 3832 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3833 | } |
| 3834 | else if (**arg == '.' && (*arg)[1] != '.') |
| 3835 | { |
| 3836 | char_u *p; |
| 3837 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3838 | if (generate_tv_PUSH(cctx, bef1_tv) == FAIL |
| 3839 | || generate_tv_PUSH(cctx, bef2_tv) == FAIL |
| 3840 | || generate_tv_PUSH(cctx, new_tv) == FAIL) |
| 3841 | return FAIL; |
| 3842 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3843 | ++*arg; |
| 3844 | p = *arg; |
| 3845 | // dictionary member: dict.name |
| 3846 | if (eval_isnamec1(*p)) |
| 3847 | while (eval_isnamec(*p)) |
| 3848 | MB_PTR_ADV(p); |
| 3849 | if (p == *arg) |
| 3850 | { |
| 3851 | semsg(_(e_syntax_at), *arg); |
| 3852 | return FAIL; |
| 3853 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3854 | if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL) |
| 3855 | return FAIL; |
| 3856 | *arg = p; |
| 3857 | } |
| 3858 | else |
| 3859 | break; |
| 3860 | } |
| 3861 | |
| 3862 | // TODO - see handle_subscript(): |
| 3863 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3864 | // Don't do this when "Func" is already a partial that was bound |
| 3865 | // explicitly (pt_auto is FALSE). |
| 3866 | |
| 3867 | return OK; |
| 3868 | } |
| 3869 | |
| 3870 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3871 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3872 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3873 | * |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3874 | * If the value is a constant "new_tv" will be set. |
| 3875 | * Before instructions are generated, any "bef_tv" will generated. |
| 3876 | * |
| 3877 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3878 | */ |
| 3879 | |
| 3880 | /* |
| 3881 | * number number constant |
| 3882 | * 0zFFFFFFFF Blob constant |
| 3883 | * "string" string constant |
| 3884 | * 'string' literal string constant |
| 3885 | * &option-name option value |
| 3886 | * @r register contents |
| 3887 | * identifier variable value |
| 3888 | * function() function call |
| 3889 | * $VAR environment variable |
| 3890 | * (expression) nested expression |
| 3891 | * [expr, expr] List |
| 3892 | * {key: val, key: val} Dictionary |
| 3893 | * #{key: val, key: val} Dictionary with literal keys |
| 3894 | * |
| 3895 | * Also handle: |
| 3896 | * ! in front logical NOT |
| 3897 | * - in front unary minus |
| 3898 | * + in front unary plus (ignored) |
| 3899 | * trailing (arg) funcref/partial call |
| 3900 | * trailing [] subscript in String or List |
| 3901 | * trailing .name entry in Dictionary |
| 3902 | * trailing ->name() method call |
| 3903 | */ |
| 3904 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3905 | compile_expr7( |
| 3906 | char_u **arg, |
| 3907 | cctx_T *cctx, |
| 3908 | typval_T *bef1_tv, |
| 3909 | typval_T *bef2_tv, |
| 3910 | typval_T *new_tv) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3911 | { |
| 3912 | typval_T rettv; |
| 3913 | char_u *start_leader, *end_leader; |
| 3914 | int ret = OK; |
| 3915 | |
| 3916 | /* |
| 3917 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3918 | */ |
| 3919 | start_leader = *arg; |
| 3920 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3921 | *arg = skipwhite(*arg + 1); |
| 3922 | end_leader = *arg; |
| 3923 | |
| 3924 | rettv.v_type = VAR_UNKNOWN; |
| 3925 | switch (**arg) |
| 3926 | { |
| 3927 | /* |
| 3928 | * Number constant. |
| 3929 | */ |
| 3930 | case '0': // also for blob starting with 0z |
| 3931 | case '1': |
| 3932 | case '2': |
| 3933 | case '3': |
| 3934 | case '4': |
| 3935 | case '5': |
| 3936 | case '6': |
| 3937 | case '7': |
| 3938 | case '8': |
| 3939 | case '9': |
| 3940 | case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL) |
| 3941 | return FAIL; |
| 3942 | break; |
| 3943 | |
| 3944 | /* |
| 3945 | * String constant: "string". |
| 3946 | */ |
| 3947 | case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL) |
| 3948 | return FAIL; |
| 3949 | break; |
| 3950 | |
| 3951 | /* |
| 3952 | * Literal string constant: 'str''ing'. |
| 3953 | */ |
| 3954 | case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL) |
| 3955 | return FAIL; |
| 3956 | break; |
| 3957 | |
| 3958 | /* |
| 3959 | * Constant Vim variable. |
| 3960 | */ |
| 3961 | case 'v': get_vim_constant(arg, &rettv); |
| 3962 | ret = NOTDONE; |
| 3963 | break; |
| 3964 | |
| 3965 | /* |
| 3966 | * List: [expr, expr] |
| 3967 | */ |
| 3968 | case '[': ret = compile_list(arg, cctx); |
| 3969 | break; |
| 3970 | |
| 3971 | /* |
| 3972 | * Dictionary: #{key: val, key: val} |
| 3973 | */ |
| 3974 | case '#': if ((*arg)[1] == '{') |
| 3975 | { |
| 3976 | ++*arg; |
| 3977 | ret = compile_dict(arg, cctx, TRUE); |
| 3978 | } |
| 3979 | else |
| 3980 | ret = NOTDONE; |
| 3981 | break; |
| 3982 | |
| 3983 | /* |
| 3984 | * Lambda: {arg, arg -> expr} |
| 3985 | * Dictionary: {'key': val, 'key': val} |
| 3986 | */ |
| 3987 | case '{': { |
| 3988 | char_u *start = skipwhite(*arg + 1); |
| 3989 | |
| 3990 | // Find out what comes after the arguments. |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3991 | // TODO: pass getline function |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3992 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3993 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3994 | if (ret != FAIL && *start == '>') |
| 3995 | ret = compile_lambda(arg, cctx); |
| 3996 | else |
| 3997 | ret = compile_dict(arg, cctx, FALSE); |
| 3998 | } |
| 3999 | break; |
| 4000 | |
| 4001 | /* |
| 4002 | * Option value: &name |
| 4003 | */ |
| 4004 | case '&': ret = compile_get_option(arg, cctx); |
| 4005 | break; |
| 4006 | |
| 4007 | /* |
| 4008 | * Environment variable: $VAR. |
| 4009 | */ |
| 4010 | case '$': ret = compile_get_env(arg, cctx); |
| 4011 | break; |
| 4012 | |
| 4013 | /* |
| 4014 | * Register contents: @r. |
| 4015 | */ |
| 4016 | case '@': ret = compile_get_register(arg, cctx); |
| 4017 | break; |
| 4018 | /* |
| 4019 | * nested expression: (expression). |
| 4020 | */ |
| 4021 | case '(': *arg = skipwhite(*arg + 1); |
| 4022 | ret = compile_expr1(arg, cctx); // recursive! |
| 4023 | *arg = skipwhite(*arg); |
| 4024 | if (**arg == ')') |
| 4025 | ++*arg; |
| 4026 | else if (ret == OK) |
| 4027 | { |
| 4028 | emsg(_(e_missing_close)); |
| 4029 | ret = FAIL; |
| 4030 | } |
| 4031 | break; |
| 4032 | |
| 4033 | default: ret = NOTDONE; |
| 4034 | break; |
| 4035 | } |
| 4036 | if (ret == FAIL) |
| 4037 | return FAIL; |
| 4038 | |
| 4039 | if (rettv.v_type != VAR_UNKNOWN) |
| 4040 | { |
| 4041 | // apply the '!', '-' and '+' before the constant |
| 4042 | if (apply_leader(&rettv, start_leader, end_leader) == FAIL) |
| 4043 | { |
| 4044 | clear_tv(&rettv); |
| 4045 | return FAIL; |
| 4046 | } |
| 4047 | start_leader = end_leader; // don't apply again below |
| 4048 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4049 | // A constant expression can possibly be handled compile time. |
| 4050 | *new_tv = rettv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4051 | } |
| 4052 | else if (ret == NOTDONE) |
| 4053 | { |
| 4054 | char_u *p; |
| 4055 | int r; |
| 4056 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4057 | if (generate_tv_PUSH(cctx, bef1_tv) == FAIL |
| 4058 | || generate_tv_PUSH(cctx, bef2_tv) == FAIL) |
| 4059 | return FAIL; |
| 4060 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4061 | if (!eval_isnamec1(**arg)) |
| 4062 | { |
| 4063 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4064 | return FAIL; |
| 4065 | } |
| 4066 | |
| 4067 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4068 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4069 | if (*p == '(') |
| 4070 | r = compile_call(arg, p - *arg, cctx, 0); |
| 4071 | else |
| 4072 | r = compile_load(arg, p, cctx, TRUE); |
| 4073 | if (r == FAIL) |
| 4074 | return FAIL; |
| 4075 | } |
| 4076 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4077 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
| 4078 | bef1_tv, bef2_tv, new_tv) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4079 | return FAIL; |
| 4080 | |
| 4081 | // Now deal with prefixed '-', '+' and '!', if not done already. |
| 4082 | return compile_leader(cctx, start_leader, end_leader); |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * * number multiplication |
| 4087 | * / number division |
| 4088 | * % number modulo |
| 4089 | */ |
| 4090 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4091 | compile_expr6( |
| 4092 | char_u **arg, |
| 4093 | cctx_T *cctx, |
| 4094 | typval_T *bef_tv, |
| 4095 | typval_T *new_tv) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | { |
| 4097 | char_u *op; |
| 4098 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4099 | // get the first expression |
| 4100 | if (compile_expr7(arg, cctx, NULL, bef_tv, new_tv) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4101 | return FAIL; |
| 4102 | |
| 4103 | /* |
| 4104 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4105 | */ |
| 4106 | for (;;) |
| 4107 | { |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4108 | typval_T tv2; |
| 4109 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4110 | op = skipwhite(*arg); |
| 4111 | if (*op != '*' && *op != '/' && *op != '%') |
| 4112 | break; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4113 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4114 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4115 | { |
| 4116 | char_u buf[3]; |
| 4117 | |
| 4118 | vim_strncpy(buf, op, 1); |
| 4119 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4120 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4121 | } |
| 4122 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4123 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4124 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4125 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4126 | // get the second expression |
| 4127 | tv2.v_type = VAR_UNKNOWN; |
| 4128 | if (compile_expr7(arg, cctx, bef_tv, new_tv, &tv2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4129 | return FAIL; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4130 | if (new_tv->v_type == VAR_NUMBER && tv2.v_type == VAR_NUMBER) |
| 4131 | { |
| 4132 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4133 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4134 | // both are numbers: compute the result |
| 4135 | switch (*op) |
| 4136 | { |
| 4137 | case '*': res = new_tv->vval.v_number * tv2.vval.v_number; |
| 4138 | break; |
| 4139 | case '/': res = new_tv->vval.v_number / tv2.vval.v_number; |
| 4140 | break; |
| 4141 | case '%': res = new_tv->vval.v_number % tv2.vval.v_number; |
| 4142 | break; |
| 4143 | } |
| 4144 | new_tv->vval.v_number = res; |
| 4145 | } |
| 4146 | else |
| 4147 | { |
| 4148 | generate_tv_PUSH(cctx, new_tv); |
| 4149 | generate_tv_PUSH(cctx, &tv2); |
| 4150 | generate_two_op(cctx, op); |
| 4151 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | return OK; |
| 4155 | } |
| 4156 | |
| 4157 | /* |
| 4158 | * + number addition |
| 4159 | * - number subtraction |
| 4160 | * .. string concatenation |
| 4161 | */ |
| 4162 | static int |
| 4163 | compile_expr5(char_u **arg, cctx_T *cctx) |
| 4164 | { |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4165 | typval_T tv1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4166 | char_u *op; |
| 4167 | int oplen; |
| 4168 | |
| 4169 | // get the first variable |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4170 | tv1.v_type = VAR_UNKNOWN; |
| 4171 | if (compile_expr6(arg, cctx, NULL, &tv1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4172 | return FAIL; |
| 4173 | |
| 4174 | /* |
| 4175 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4176 | */ |
| 4177 | for (;;) |
| 4178 | { |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4179 | typval_T tv2; |
| 4180 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | op = skipwhite(*arg); |
| 4182 | if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.'))) |
| 4183 | break; |
| 4184 | oplen = (*op == '.' ? 2 : 1); |
| 4185 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4186 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4187 | { |
| 4188 | char_u buf[3]; |
| 4189 | |
| 4190 | vim_strncpy(buf, op, oplen); |
| 4191 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4192 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4196 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4197 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4198 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4199 | // get the second expression |
| 4200 | tv2.v_type = VAR_UNKNOWN; |
| 4201 | if (compile_expr6(arg, cctx, &tv1, &tv2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4202 | return FAIL; |
| 4203 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4204 | if (*op == '+' && tv1.v_type == VAR_NUMBER && tv2.v_type == VAR_NUMBER) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | { |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4206 | // add constant numbers |
| 4207 | tv1.vval.v_number = tv1.vval.v_number + tv2.vval.v_number; |
| 4208 | } |
| 4209 | else if (*op == '-' && tv1.v_type == VAR_NUMBER |
| 4210 | && tv2.v_type == VAR_NUMBER) |
| 4211 | { |
| 4212 | // subtract constant numbers |
| 4213 | tv1.vval.v_number = tv1.vval.v_number - tv2.vval.v_number; |
| 4214 | } |
| 4215 | else if (*op == '.' && tv1.v_type == VAR_STRING |
| 4216 | && tv2.v_type == VAR_STRING) |
| 4217 | { |
| 4218 | // concatenate constant strings |
| 4219 | char_u *s1 = tv1.vval.v_string; |
| 4220 | char_u *s2 = tv2.vval.v_string; |
| 4221 | size_t len1 = STRLEN(s1); |
| 4222 | |
| 4223 | tv1.vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4224 | if (tv1.vval.v_string == NULL) |
| 4225 | { |
| 4226 | vim_free(s1); |
| 4227 | vim_free(s2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4228 | return FAIL; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4229 | } |
| 4230 | mch_memmove(tv1.vval.v_string, s1, len1); |
| 4231 | STRCPY(tv1.vval.v_string + len1, s2); |
Bram Moolenaar | cca34aa | 2020-05-07 22:23:58 +0200 | [diff] [blame^] | 4232 | vim_free(s1); |
| 4233 | vim_free(s2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4234 | } |
| 4235 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4236 | { |
| 4237 | generate_tv_PUSH(cctx, &tv1); |
| 4238 | generate_tv_PUSH(cctx, &tv2); |
| 4239 | if (*op == '.') |
| 4240 | { |
| 4241 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4242 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4243 | return FAIL; |
| 4244 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4245 | } |
| 4246 | else |
| 4247 | generate_two_op(cctx, op); |
| 4248 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4249 | } |
| 4250 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4251 | // TODO: move to caller |
| 4252 | generate_tv_PUSH(cctx, &tv1); |
| 4253 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4254 | return OK; |
| 4255 | } |
| 4256 | |
| 4257 | /* |
| 4258 | * expr5a == expr5b |
| 4259 | * expr5a =~ expr5b |
| 4260 | * expr5a != expr5b |
| 4261 | * expr5a !~ expr5b |
| 4262 | * expr5a > expr5b |
| 4263 | * expr5a >= expr5b |
| 4264 | * expr5a < expr5b |
| 4265 | * expr5a <= expr5b |
| 4266 | * expr5a is expr5b |
| 4267 | * expr5a isnot expr5b |
| 4268 | * |
| 4269 | * Produces instructions: |
| 4270 | * EVAL expr5a Push result of "expr5a" |
| 4271 | * EVAL expr5b Push result of "expr5b" |
| 4272 | * COMPARE one of the compare instructions |
| 4273 | */ |
| 4274 | static int |
| 4275 | compile_expr4(char_u **arg, cctx_T *cctx) |
| 4276 | { |
| 4277 | exptype_T type = EXPR_UNKNOWN; |
| 4278 | char_u *p; |
| 4279 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4280 | int type_is = FALSE; |
| 4281 | |
| 4282 | // get the first variable |
| 4283 | if (compile_expr5(arg, cctx) == FAIL) |
| 4284 | return FAIL; |
| 4285 | |
| 4286 | p = skipwhite(*arg); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4287 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | |
| 4289 | /* |
| 4290 | * If there is a comparative operator, use it. |
| 4291 | */ |
| 4292 | if (type != EXPR_UNKNOWN) |
| 4293 | { |
| 4294 | int ic = FALSE; // Default: do not ignore case |
| 4295 | |
| 4296 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4297 | { |
| 4298 | semsg(_(e_invexpr2), *arg); |
| 4299 | return FAIL; |
| 4300 | } |
| 4301 | // extra question mark appended: ignore case |
| 4302 | if (p[len] == '?') |
| 4303 | { |
| 4304 | ic = TRUE; |
| 4305 | ++len; |
| 4306 | } |
| 4307 | // extra '#' appended: match case (ignored) |
| 4308 | else if (p[len] == '#') |
| 4309 | ++len; |
| 4310 | // nothing appended: match case |
| 4311 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4312 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4313 | { |
| 4314 | char_u buf[7]; |
| 4315 | |
| 4316 | vim_strncpy(buf, p, len); |
| 4317 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4318 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | // get the second variable |
| 4322 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4323 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4324 | return FAIL; |
| 4325 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | if (compile_expr5(arg, cctx) == FAIL) |
| 4327 | return FAIL; |
| 4328 | |
| 4329 | generate_COMPARE(cctx, type, ic); |
| 4330 | } |
| 4331 | |
| 4332 | return OK; |
| 4333 | } |
| 4334 | |
| 4335 | /* |
| 4336 | * Compile || or &&. |
| 4337 | */ |
| 4338 | static int |
| 4339 | compile_and_or(char_u **arg, cctx_T *cctx, char *op) |
| 4340 | { |
| 4341 | char_u *p = skipwhite(*arg); |
| 4342 | int opchar = *op; |
| 4343 | |
| 4344 | if (p[0] == opchar && p[1] == opchar) |
| 4345 | { |
| 4346 | garray_T *instr = &cctx->ctx_instr; |
| 4347 | garray_T end_ga; |
| 4348 | |
| 4349 | /* |
| 4350 | * Repeat until there is no following "||" or "&&" |
| 4351 | */ |
| 4352 | ga_init2(&end_ga, sizeof(int), 10); |
| 4353 | while (p[0] == opchar && p[1] == opchar) |
| 4354 | { |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4355 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4356 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4357 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4358 | return FAIL; |
| 4359 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4360 | |
| 4361 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4362 | { |
| 4363 | ga_clear(&end_ga); |
| 4364 | return FAIL; |
| 4365 | } |
| 4366 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4367 | ++end_ga.ga_len; |
| 4368 | generate_JUMP(cctx, opchar == '|' |
| 4369 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4370 | |
| 4371 | // eval the next expression |
| 4372 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4373 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4374 | return FAIL; |
| 4375 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4376 | if ((opchar == '|' ? compile_expr3(arg, cctx) |
| 4377 | : compile_expr4(arg, cctx)) == FAIL) |
| 4378 | { |
| 4379 | ga_clear(&end_ga); |
| 4380 | return FAIL; |
| 4381 | } |
| 4382 | p = skipwhite(*arg); |
| 4383 | } |
| 4384 | |
| 4385 | // Fill in the end label in all jumps. |
| 4386 | while (end_ga.ga_len > 0) |
| 4387 | { |
| 4388 | isn_T *isn; |
| 4389 | |
| 4390 | --end_ga.ga_len; |
| 4391 | isn = ((isn_T *)instr->ga_data) |
| 4392 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4393 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4394 | } |
| 4395 | ga_clear(&end_ga); |
| 4396 | } |
| 4397 | |
| 4398 | return OK; |
| 4399 | } |
| 4400 | |
| 4401 | /* |
| 4402 | * expr4a && expr4a && expr4a logical AND |
| 4403 | * |
| 4404 | * Produces instructions: |
| 4405 | * EVAL expr4a Push result of "expr4a" |
| 4406 | * JUMP_AND_KEEP_IF_FALSE end |
| 4407 | * EVAL expr4b Push result of "expr4b" |
| 4408 | * JUMP_AND_KEEP_IF_FALSE end |
| 4409 | * EVAL expr4c Push result of "expr4c" |
| 4410 | * end: |
| 4411 | */ |
| 4412 | static int |
| 4413 | compile_expr3(char_u **arg, cctx_T *cctx) |
| 4414 | { |
| 4415 | // get the first variable |
| 4416 | if (compile_expr4(arg, cctx) == FAIL) |
| 4417 | return FAIL; |
| 4418 | |
| 4419 | // || and && work almost the same |
| 4420 | return compile_and_or(arg, cctx, "&&"); |
| 4421 | } |
| 4422 | |
| 4423 | /* |
| 4424 | * expr3a || expr3b || expr3c logical OR |
| 4425 | * |
| 4426 | * Produces instructions: |
| 4427 | * EVAL expr3a Push result of "expr3a" |
| 4428 | * JUMP_AND_KEEP_IF_TRUE end |
| 4429 | * EVAL expr3b Push result of "expr3b" |
| 4430 | * JUMP_AND_KEEP_IF_TRUE end |
| 4431 | * EVAL expr3c Push result of "expr3c" |
| 4432 | * end: |
| 4433 | */ |
| 4434 | static int |
| 4435 | compile_expr2(char_u **arg, cctx_T *cctx) |
| 4436 | { |
| 4437 | // eval the first expression |
| 4438 | if (compile_expr3(arg, cctx) == FAIL) |
| 4439 | return FAIL; |
| 4440 | |
| 4441 | // || and && work almost the same |
| 4442 | return compile_and_or(arg, cctx, "||"); |
| 4443 | } |
| 4444 | |
| 4445 | /* |
| 4446 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4447 | * |
| 4448 | * Produces instructions: |
| 4449 | * EVAL expr2 Push result of "expr" |
| 4450 | * JUMP_IF_FALSE alt jump if false |
| 4451 | * EVAL expr1a |
| 4452 | * JUMP_ALWAYS end |
| 4453 | * alt: EVAL expr1b |
| 4454 | * end: |
| 4455 | */ |
| 4456 | static int |
| 4457 | compile_expr1(char_u **arg, cctx_T *cctx) |
| 4458 | { |
| 4459 | char_u *p; |
| 4460 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4461 | // Evaluate the first expression. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4462 | if (compile_expr2(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4463 | return FAIL; |
| 4464 | |
| 4465 | p = skipwhite(*arg); |
| 4466 | if (*p == '?') |
| 4467 | { |
| 4468 | garray_T *instr = &cctx->ctx_instr; |
| 4469 | garray_T *stack = &cctx->ctx_type_stack; |
| 4470 | int alt_idx = instr->ga_len; |
| 4471 | int end_idx; |
| 4472 | isn_T *isn; |
| 4473 | type_T *type1; |
| 4474 | type_T *type2; |
| 4475 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4476 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4477 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4478 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4479 | return FAIL; |
| 4480 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4481 | |
| 4482 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4483 | |
| 4484 | // evaluate the second expression; any type is accepted |
| 4485 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4486 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4487 | return FAIL; |
| 4488 | |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4489 | if (compile_expr1(arg, cctx) == FAIL) |
| 4490 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4491 | |
| 4492 | // remember the type and drop it |
| 4493 | --stack->ga_len; |
| 4494 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 4495 | |
| 4496 | end_idx = instr->ga_len; |
| 4497 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4498 | |
| 4499 | // jump here from JUMP_IF_FALSE |
| 4500 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4501 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4502 | |
| 4503 | // Check for the ":". |
| 4504 | p = skipwhite(*arg); |
| 4505 | if (*p != ':') |
| 4506 | { |
| 4507 | emsg(_(e_missing_colon)); |
| 4508 | return FAIL; |
| 4509 | } |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4510 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4511 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4512 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4513 | return FAIL; |
| 4514 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4515 | |
| 4516 | // evaluate the third expression |
| 4517 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4518 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4519 | return FAIL; |
| 4520 | |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4521 | if (compile_expr1(arg, cctx) == FAIL) |
| 4522 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4523 | |
| 4524 | // If the types differ, the result has a more generic type. |
| 4525 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 4526 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4527 | |
| 4528 | // jump here from JUMP_ALWAYS |
| 4529 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4530 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4531 | } |
| 4532 | return OK; |
| 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * compile "return [expr]" |
| 4537 | */ |
| 4538 | static char_u * |
| 4539 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4540 | { |
| 4541 | char_u *p = arg; |
| 4542 | garray_T *stack = &cctx->ctx_type_stack; |
| 4543 | type_T *stack_type; |
| 4544 | |
| 4545 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4546 | { |
| 4547 | // compile return argument into instructions |
| 4548 | if (compile_expr1(&p, cctx) == FAIL) |
| 4549 | return NULL; |
| 4550 | |
| 4551 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4552 | if (set_return_type) |
| 4553 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
| 4554 | else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx) |
| 4555 | == FAIL) |
| 4556 | return NULL; |
| 4557 | } |
| 4558 | else |
| 4559 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4560 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4561 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4562 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4563 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4564 | { |
| 4565 | emsg(_("E1003: Missing return value")); |
| 4566 | return NULL; |
| 4567 | } |
| 4568 | |
| 4569 | // No argument, return zero. |
| 4570 | generate_PUSHNR(cctx, 0); |
| 4571 | } |
| 4572 | |
| 4573 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4574 | return NULL; |
| 4575 | |
| 4576 | // "return val | endif" is possible |
| 4577 | return skipwhite(p); |
| 4578 | } |
| 4579 | |
| 4580 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4581 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4582 | * Return a pointer to the line in allocated memory. |
| 4583 | * Return NULL for end-of-file or some error. |
| 4584 | */ |
| 4585 | static char_u * |
| 4586 | exarg_getline( |
| 4587 | int c UNUSED, |
| 4588 | void *cookie, |
| 4589 | int indent UNUSED, |
| 4590 | int do_concat UNUSED) |
| 4591 | { |
| 4592 | cctx_T *cctx = (cctx_T *)cookie; |
| 4593 | |
| 4594 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4595 | { |
| 4596 | iemsg("Heredoc got to end"); |
| 4597 | return NULL; |
| 4598 | } |
| 4599 | ++cctx->ctx_lnum; |
| 4600 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4601 | [cctx->ctx_lnum]); |
| 4602 | } |
| 4603 | |
| 4604 | /* |
| 4605 | * Compile a nested :def command. |
| 4606 | */ |
| 4607 | static char_u * |
| 4608 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4609 | { |
| 4610 | char_u *name_start = eap->arg; |
| 4611 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4612 | char_u *name = get_lambda_name(); |
| 4613 | lvar_T *lvar; |
| 4614 | ufunc_T *ufunc; |
| 4615 | |
| 4616 | eap->arg = name_end; |
| 4617 | eap->getline = exarg_getline; |
| 4618 | eap->cookie = cctx; |
| 4619 | eap->skip = cctx->ctx_skip == TRUE; |
| 4620 | eap->forceit = FALSE; |
| 4621 | ufunc = def_function(eap, name, cctx); |
| 4622 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4623 | if (ufunc == NULL || ufunc->uf_dfunc_idx < 0) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4624 | return NULL; |
| 4625 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4626 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4627 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4628 | TRUE, ufunc->uf_func_type); |
| 4629 | |
| 4630 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4631 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4632 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4633 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4634 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4635 | return (char_u *)""; |
| 4636 | } |
| 4637 | |
| 4638 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4639 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4640 | */ |
| 4641 | int |
| 4642 | assignment_len(char_u *p, int *heredoc) |
| 4643 | { |
| 4644 | if (*p == '=') |
| 4645 | { |
| 4646 | if (p[1] == '<' && p[2] == '<') |
| 4647 | { |
| 4648 | *heredoc = TRUE; |
| 4649 | return 3; |
| 4650 | } |
| 4651 | return 1; |
| 4652 | } |
| 4653 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4654 | return 2; |
| 4655 | if (STRNCMP(p, "..=", 3) == 0) |
| 4656 | return 3; |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
| 4660 | // words that cannot be used as a variable |
| 4661 | static char *reserved[] = { |
| 4662 | "true", |
| 4663 | "false", |
| 4664 | NULL |
| 4665 | }; |
| 4666 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4667 | typedef enum { |
| 4668 | dest_local, |
| 4669 | dest_option, |
| 4670 | dest_env, |
| 4671 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4672 | dest_buffer, |
| 4673 | dest_window, |
| 4674 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4675 | dest_vimvar, |
| 4676 | dest_script, |
| 4677 | dest_reg, |
| 4678 | } assign_dest_T; |
| 4679 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4680 | /* |
| 4681 | * compile "let var [= expr]", "const var = expr" and "var = expr" |
| 4682 | * "arg" points to "var". |
| 4683 | */ |
| 4684 | static char_u * |
| 4685 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4686 | { |
| 4687 | char_u *p; |
| 4688 | char_u *ret = NULL; |
| 4689 | int var_count = 0; |
| 4690 | int semicolon = 0; |
| 4691 | size_t varlen; |
| 4692 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4693 | int new_local = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4694 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4695 | int opt_type; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4696 | assign_dest_T dest = dest_local; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4697 | int opt_flags = 0; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 4698 | int vimvaridx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4699 | int oplen = 0; |
| 4700 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4701 | type_T *type = &t_any; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4702 | lvar_T *lvar = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4703 | char_u *name; |
| 4704 | char_u *sp; |
| 4705 | int has_type = FALSE; |
| 4706 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
| 4707 | int instr_count = -1; |
| 4708 | |
| 4709 | p = skip_var_list(arg, FALSE, &var_count, &semicolon); |
| 4710 | if (p == NULL) |
| 4711 | return NULL; |
| 4712 | if (var_count > 0) |
| 4713 | { |
| 4714 | // TODO: let [var, var] = list |
| 4715 | emsg("Cannot handle a list yet"); |
| 4716 | return NULL; |
| 4717 | } |
| 4718 | |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4719 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4720 | if (is_decl && p == arg + 2 && p[-1] == ':') |
| 4721 | --p; |
| 4722 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4723 | varlen = p - arg; |
| 4724 | name = vim_strnsave(arg, (int)varlen); |
| 4725 | if (name == NULL) |
| 4726 | return NULL; |
| 4727 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4728 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4729 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4730 | if (*arg == '&') |
| 4731 | { |
| 4732 | int cc; |
| 4733 | long numval; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4734 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4735 | dest = dest_option; |
| 4736 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4737 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4738 | emsg(_(e_const_option)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4739 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4740 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4741 | if (is_decl) |
| 4742 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4743 | semsg(_("E1052: Cannot declare an option: %s"), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4744 | goto theend; |
| 4745 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4746 | p = arg; |
| 4747 | p = find_option_end(&p, &opt_flags); |
| 4748 | if (p == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4749 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4750 | // cannot happen? |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4751 | emsg(_(e_letunexp)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4752 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4753 | } |
| 4754 | cc = *p; |
| 4755 | *p = NUL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4756 | opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4757 | *p = cc; |
| 4758 | if (opt_type == -3) |
| 4759 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4760 | semsg(_(e_unknown_option), arg); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4761 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4762 | } |
| 4763 | if (opt_type == -2 || opt_type == 0) |
| 4764 | type = &t_string; |
| 4765 | else |
| 4766 | type = &t_number; // both number and boolean option |
| 4767 | } |
| 4768 | else if (*arg == '$') |
| 4769 | { |
| 4770 | dest = dest_env; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4771 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4772 | if (is_decl) |
| 4773 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4774 | semsg(_("E1065: Cannot declare an environment variable: %s"), |
| 4775 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4776 | goto theend; |
| 4777 | } |
| 4778 | } |
| 4779 | else if (*arg == '@') |
| 4780 | { |
| 4781 | if (!valid_yank_reg(arg[1], TRUE)) |
| 4782 | { |
| 4783 | emsg_invreg(arg[1]); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4784 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4785 | } |
| 4786 | dest = dest_reg; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4787 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4788 | if (is_decl) |
| 4789 | { |
| 4790 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 4791 | goto theend; |
| 4792 | } |
| 4793 | } |
| 4794 | else if (STRNCMP(arg, "g:", 2) == 0) |
| 4795 | { |
| 4796 | dest = dest_global; |
| 4797 | if (is_decl) |
| 4798 | { |
| 4799 | semsg(_("E1016: Cannot declare a global variable: %s"), name); |
| 4800 | goto theend; |
| 4801 | } |
| 4802 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4803 | else if (STRNCMP(arg, "b:", 2) == 0) |
| 4804 | { |
| 4805 | dest = dest_buffer; |
| 4806 | if (is_decl) |
| 4807 | { |
| 4808 | semsg(_("E1078: Cannot declare a buffer variable: %s"), name); |
| 4809 | goto theend; |
| 4810 | } |
| 4811 | } |
| 4812 | else if (STRNCMP(arg, "w:", 2) == 0) |
| 4813 | { |
| 4814 | dest = dest_window; |
| 4815 | if (is_decl) |
| 4816 | { |
| 4817 | semsg(_("E1079: Cannot declare a window variable: %s"), name); |
| 4818 | goto theend; |
| 4819 | } |
| 4820 | } |
| 4821 | else if (STRNCMP(arg, "t:", 2) == 0) |
| 4822 | { |
| 4823 | dest = dest_tab; |
| 4824 | if (is_decl) |
| 4825 | { |
| 4826 | semsg(_("E1080: Cannot declare a tab variable: %s"), name); |
| 4827 | goto theend; |
| 4828 | } |
| 4829 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4830 | else if (STRNCMP(arg, "v:", 2) == 0) |
| 4831 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4832 | typval_T *vtv; |
| 4833 | int di_flags; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4834 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4835 | vimvaridx = find_vim_var(name + 2, &di_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4836 | if (vimvaridx < 0) |
| 4837 | { |
| 4838 | semsg(_(e_var_notfound), arg); |
| 4839 | goto theend; |
| 4840 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4841 | // We use the current value of "sandbox" here, is that OK? |
| 4842 | if (var_check_ro(di_flags, name, FALSE)) |
| 4843 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4844 | dest = dest_vimvar; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4845 | vtv = get_vim_var_tv(vimvaridx); |
| 4846 | type = typval2type(vtv); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4847 | if (is_decl) |
| 4848 | { |
| 4849 | semsg(_("E1064: Cannot declare a v: variable: %s"), name); |
| 4850 | goto theend; |
| 4851 | } |
| 4852 | } |
| 4853 | else |
| 4854 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4855 | int idx; |
| 4856 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4857 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4858 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4859 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4860 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4861 | goto theend; |
| 4862 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4863 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4864 | lvar = lookup_local(arg, varlen, cctx); |
| 4865 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4866 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4867 | if (is_decl) |
| 4868 | { |
| 4869 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4870 | goto theend; |
| 4871 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4872 | else if (lvar->lv_const) |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4873 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4874 | semsg(_("E1018: Cannot assign to a constant: %s"), name); |
| 4875 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4876 | } |
| 4877 | } |
| 4878 | else if (STRNCMP(arg, "s:", 2) == 0 |
| 4879 | || lookup_script(arg, varlen) == OK |
| 4880 | || find_imported(arg, varlen, cctx) != NULL) |
| 4881 | { |
| 4882 | dest = dest_script; |
| 4883 | if (is_decl) |
| 4884 | { |
| 4885 | semsg(_("E1054: Variable already declared in the script: %s"), |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4886 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4887 | goto theend; |
| 4888 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4889 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 4890 | else if (name[1] == ':' && name[2] != NUL) |
| 4891 | { |
| 4892 | semsg(_("E1082: Cannot use a namespaced variable: %s"), name); |
| 4893 | goto theend; |
| 4894 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4895 | } |
| 4896 | } |
| 4897 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4898 | if (dest != dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4899 | { |
| 4900 | if (is_decl && *p == ':') |
| 4901 | { |
| 4902 | // parse optional type: "let var: type = expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 4903 | if (!VIM_ISWHITE(p[1])) |
| 4904 | { |
| 4905 | semsg(_(e_white_after), ":"); |
| 4906 | goto theend; |
| 4907 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4908 | p = skipwhite(p + 1); |
| 4909 | type = parse_type(&p, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4910 | has_type = TRUE; |
| 4911 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4912 | else if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4913 | type = lvar->lv_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | sp = p; |
| 4917 | p = skipwhite(p); |
| 4918 | op = p; |
| 4919 | oplen = assignment_len(p, &heredoc); |
| 4920 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4921 | { |
| 4922 | char_u buf[4]; |
| 4923 | |
| 4924 | vim_strncpy(buf, op, oplen); |
| 4925 | semsg(_(e_white_both), buf); |
| 4926 | } |
| 4927 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4928 | if (oplen == 3 && !heredoc && dest != dest_global |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4929 | && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4930 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 4931 | emsg(_("E1019: Can only concatenate to string")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4932 | goto theend; |
| 4933 | } |
| 4934 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4935 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4936 | { |
| 4937 | if (oplen > 1 && !heredoc) |
| 4938 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4939 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4940 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4941 | name); |
| 4942 | goto theend; |
| 4943 | } |
| 4944 | |
| 4945 | // new local variable |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 4946 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4947 | goto theend; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4948 | lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type); |
| 4949 | if (lvar == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4950 | goto theend; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4951 | new_local = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | if (heredoc) |
| 4955 | { |
| 4956 | list_T *l; |
| 4957 | listitem_T *li; |
| 4958 | |
| 4959 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4960 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4961 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4962 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4963 | |
| 4964 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4965 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4966 | { |
| 4967 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4968 | li->li_tv.vval.v_string = NULL; |
| 4969 | } |
| 4970 | generate_NEWLIST(cctx, l->lv_len); |
| 4971 | type = &t_list_string; |
| 4972 | list_free(l); |
| 4973 | p += STRLEN(p); |
| 4974 | } |
| 4975 | else if (oplen > 0) |
| 4976 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4977 | int r; |
| 4978 | type_T *stacktype; |
| 4979 | garray_T *stack; |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4980 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4981 | // for "+=", "*=", "..=" etc. first load the current value |
| 4982 | if (*op != '=') |
| 4983 | { |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4984 | switch (dest) |
| 4985 | { |
| 4986 | case dest_option: |
| 4987 | // TODO: check the option exists |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4988 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4989 | break; |
| 4990 | case dest_global: |
| 4991 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4992 | break; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4993 | case dest_buffer: |
| 4994 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4995 | break; |
| 4996 | case dest_window: |
| 4997 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4998 | break; |
| 4999 | case dest_tab: |
| 5000 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 5001 | break; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5002 | case dest_script: |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 5003 | compile_load_scriptvar(cctx, |
| 5004 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5005 | break; |
| 5006 | case dest_env: |
| 5007 | // Include $ in the name here |
| 5008 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 5009 | break; |
| 5010 | case dest_reg: |
| 5011 | generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string); |
| 5012 | break; |
| 5013 | case dest_vimvar: |
| 5014 | generate_LOADV(cctx, name + 2, TRUE); |
| 5015 | break; |
| 5016 | case dest_local: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 5017 | if (lvar->lv_from_outer) |
| 5018 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 5019 | NULL, type); |
| 5020 | else |
| 5021 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5022 | break; |
| 5023 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5024 | } |
| 5025 | |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5026 | // Compile the expression. Temporarily hide the new local variable |
| 5027 | // here, it is not available to this expression. |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 5028 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5029 | --cctx->ctx_locals.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5030 | instr_count = instr->ga_len; |
| 5031 | p = skipwhite(p + oplen); |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5032 | r = compile_expr1(&p, cctx); |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 5033 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5034 | ++cctx->ctx_locals.ga_len; |
| 5035 | if (r == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5036 | goto theend; |
| 5037 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5038 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5039 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5040 | stack = &cctx->ctx_type_stack; |
| 5041 | stacktype = stack->ga_len == 0 ? &t_void |
| 5042 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5043 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5044 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5045 | if (new_local && !has_type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5046 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5047 | if (stacktype->tt_type == VAR_VOID) |
| 5048 | { |
| 5049 | emsg(_("E1031: Cannot use void value")); |
| 5050 | goto theend; |
| 5051 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5052 | else |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5053 | { |
| 5054 | // An empty list or dict has a &t_void member, for a |
| 5055 | // variable that implies &t_any. |
| 5056 | if (stacktype == &t_list_empty) |
| 5057 | lvar->lv_type = &t_list_any; |
| 5058 | else if (stacktype == &t_dict_empty) |
| 5059 | lvar->lv_type = &t_dict_any; |
| 5060 | else |
| 5061 | lvar->lv_type = stacktype; |
| 5062 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5063 | } |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5064 | else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL) |
| 5065 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5066 | } |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5067 | else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5068 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5069 | } |
| 5070 | } |
| 5071 | else if (cmdidx == CMD_const) |
| 5072 | { |
| 5073 | emsg(_("E1021: const requires a value")); |
| 5074 | goto theend; |
| 5075 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5076 | else if (!has_type || dest == dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5077 | { |
| 5078 | emsg(_("E1022: type or initialization required")); |
| 5079 | goto theend; |
| 5080 | } |
| 5081 | else |
| 5082 | { |
| 5083 | // variables are always initialized |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5084 | if (ga_grow(instr, 1) == FAIL) |
| 5085 | goto theend; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5086 | switch (type->tt_type) |
| 5087 | { |
| 5088 | case VAR_BOOL: |
| 5089 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5090 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5091 | case VAR_FLOAT: |
| 5092 | #ifdef FEAT_FLOAT |
| 5093 | generate_PUSHF(cctx, 0.0); |
| 5094 | #endif |
| 5095 | break; |
| 5096 | case VAR_STRING: |
| 5097 | generate_PUSHS(cctx, NULL); |
| 5098 | break; |
| 5099 | case VAR_BLOB: |
| 5100 | generate_PUSHBLOB(cctx, NULL); |
| 5101 | break; |
| 5102 | case VAR_FUNC: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5103 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5104 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5105 | case VAR_LIST: |
| 5106 | generate_NEWLIST(cctx, 0); |
| 5107 | break; |
| 5108 | case VAR_DICT: |
| 5109 | generate_NEWDICT(cctx, 0); |
| 5110 | break; |
| 5111 | case VAR_JOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 5112 | generate_PUSHJOB(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5113 | break; |
| 5114 | case VAR_CHANNEL: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 5115 | generate_PUSHCHANNEL(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5116 | break; |
| 5117 | case VAR_NUMBER: |
| 5118 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5119 | case VAR_ANY: |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 5120 | case VAR_PARTIAL: |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5121 | case VAR_VOID: |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 5122 | case VAR_SPECIAL: // cannot happen |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5123 | generate_PUSHNR(cctx, 0); |
| 5124 | break; |
| 5125 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | if (oplen > 0 && *op != '=') |
| 5129 | { |
| 5130 | type_T *expected = &t_number; |
| 5131 | garray_T *stack = &cctx->ctx_type_stack; |
| 5132 | type_T *stacktype; |
| 5133 | |
| 5134 | // TODO: if type is known use float or any operation |
| 5135 | |
| 5136 | if (*op == '.') |
| 5137 | expected = &t_string; |
| 5138 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5139 | if (need_type(stacktype, expected, -1, cctx) == FAIL) |
| 5140 | goto theend; |
| 5141 | |
| 5142 | if (*op == '.') |
| 5143 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5144 | else |
| 5145 | { |
| 5146 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5147 | |
| 5148 | if (isn == NULL) |
| 5149 | goto theend; |
| 5150 | switch (*op) |
| 5151 | { |
| 5152 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5153 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5154 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5155 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5156 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5157 | } |
| 5158 | } |
| 5159 | } |
| 5160 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5161 | switch (dest) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5162 | { |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5163 | case dest_option: |
| 5164 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5165 | break; |
| 5166 | case dest_global: |
| 5167 | // include g: with the name, easier to execute that way |
| 5168 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5169 | break; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 5170 | case dest_buffer: |
| 5171 | // include b: with the name, easier to execute that way |
| 5172 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5173 | break; |
| 5174 | case dest_window: |
| 5175 | // include w: with the name, easier to execute that way |
| 5176 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5177 | break; |
| 5178 | case dest_tab: |
| 5179 | // include t: with the name, easier to execute that way |
| 5180 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5181 | break; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5182 | case dest_env: |
| 5183 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5184 | break; |
| 5185 | case dest_reg: |
| 5186 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5187 | break; |
| 5188 | case dest_vimvar: |
| 5189 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5190 | break; |
| 5191 | case dest_script: |
| 5192 | { |
| 5193 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5194 | imported_T *import = NULL; |
| 5195 | int sid = current_sctx.sc_sid; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5196 | int idx; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 5197 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5198 | if (name[1] != ':') |
| 5199 | { |
| 5200 | import = find_imported(name, 0, cctx); |
| 5201 | if (import != NULL) |
| 5202 | sid = import->imp_sid; |
| 5203 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5204 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5205 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5206 | // TODO: specific type |
| 5207 | if (idx < 0) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5208 | { |
| 5209 | char_u *name_s = name; |
| 5210 | |
| 5211 | // Include s: in the name for store_var() |
| 5212 | if (name[1] != ':') |
| 5213 | { |
| 5214 | int len = (int)STRLEN(name) + 3; |
| 5215 | |
| 5216 | name_s = alloc(len); |
| 5217 | if (name_s == NULL) |
| 5218 | name_s = name; |
| 5219 | else |
| 5220 | vim_snprintf((char *)name_s, len, "s:%s", name); |
| 5221 | } |
| 5222 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any); |
| 5223 | if (name_s != name) |
| 5224 | vim_free(name_s); |
| 5225 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5226 | else |
| 5227 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 5228 | sid, idx, &t_any); |
| 5229 | } |
| 5230 | break; |
| 5231 | case dest_local: |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5232 | if (lvar != NULL) |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5233 | { |
| 5234 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5235 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5236 | // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE |
| 5237 | // into ISN_STORENR |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 5238 | if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1 |
| 5239 | && isn->isn_type == ISN_PUSHNR) |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5240 | { |
| 5241 | varnumber_T val = isn->isn_arg.number; |
| 5242 | garray_T *stack = &cctx->ctx_type_stack; |
| 5243 | |
| 5244 | isn->isn_type = ISN_STORENR; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5245 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 5246 | isn->isn_arg.storenr.stnr_val = val; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5247 | if (stack->ga_len > 0) |
| 5248 | --stack->ga_len; |
| 5249 | } |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 5250 | else if (lvar->lv_from_outer) |
| 5251 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5252 | else |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5253 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5254 | } |
| 5255 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5256 | } |
| 5257 | ret = p; |
| 5258 | |
| 5259 | theend: |
| 5260 | vim_free(name); |
| 5261 | return ret; |
| 5262 | } |
| 5263 | |
| 5264 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5265 | * Check if "name" can be "unlet". |
| 5266 | */ |
| 5267 | int |
| 5268 | check_vim9_unlet(char_u *name) |
| 5269 | { |
| 5270 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5271 | { |
| 5272 | semsg(_("E1081: Cannot unlet %s"), name); |
| 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 | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 5347 | return handle_import(arg, &cctx->ctx_imports, 0, 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; |
| 5483 | scope_T *scope; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5484 | typval_T tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5485 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5486 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 5487 | tv.v_type = VAR_UNKNOWN; |
| 5488 | if (evaluate_const_expr1(&p, cctx, &tv) == OK) |
| 5489 | cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE; |
| 5490 | else |
| 5491 | cctx->ctx_skip = MAYBE; |
| 5492 | clear_tv(&tv); |
| 5493 | if (cctx->ctx_skip == MAYBE) |
| 5494 | { |
| 5495 | p = arg; |
| 5496 | if (compile_expr1(&p, cctx) == FAIL) |
| 5497 | return NULL; |
| 5498 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5499 | |
| 5500 | scope = new_scope(cctx, IF_SCOPE); |
| 5501 | if (scope == NULL) |
| 5502 | return NULL; |
| 5503 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5504 | if (cctx->ctx_skip == MAYBE) |
| 5505 | { |
| 5506 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5507 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5508 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5509 | } |
| 5510 | else |
| 5511 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5512 | |
| 5513 | return p; |
| 5514 | } |
| 5515 | |
| 5516 | static char_u * |
| 5517 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5518 | { |
| 5519 | char_u *p = arg; |
| 5520 | garray_T *instr = &cctx->ctx_instr; |
| 5521 | isn_T *isn; |
| 5522 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5523 | typval_T tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5524 | |
| 5525 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5526 | { |
| 5527 | emsg(_(e_elseif_without_if)); |
| 5528 | return NULL; |
| 5529 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5530 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5531 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5532 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5533 | { |
| 5534 | 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] | 5535 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5536 | return NULL; |
| 5537 | // previous "if" or "elseif" jumps here |
| 5538 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5539 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5540 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5541 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5542 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 5543 | tv.v_type = VAR_UNKNOWN; |
| 5544 | if (evaluate_const_expr1(&p, cctx, &tv) == OK) |
| 5545 | cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE; |
| 5546 | else |
| 5547 | cctx->ctx_skip = MAYBE; |
| 5548 | clear_tv(&tv); |
| 5549 | if (cctx->ctx_skip == MAYBE) |
| 5550 | { |
| 5551 | p = arg; |
| 5552 | if (compile_expr1(&p, cctx) == FAIL) |
| 5553 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5554 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5555 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5556 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5557 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5558 | } |
| 5559 | else |
| 5560 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5561 | |
| 5562 | return p; |
| 5563 | } |
| 5564 | |
| 5565 | static char_u * |
| 5566 | compile_else(char_u *arg, cctx_T *cctx) |
| 5567 | { |
| 5568 | char_u *p = arg; |
| 5569 | garray_T *instr = &cctx->ctx_instr; |
| 5570 | isn_T *isn; |
| 5571 | scope_T *scope = cctx->ctx_scope; |
| 5572 | |
| 5573 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5574 | { |
| 5575 | emsg(_(e_else_without_if)); |
| 5576 | return NULL; |
| 5577 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5578 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5579 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5580 | // jump from previous block to the end, unless the else block is empty |
| 5581 | if (cctx->ctx_skip == MAYBE) |
| 5582 | { |
| 5583 | 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] | 5584 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5585 | return NULL; |
| 5586 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5587 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5588 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5589 | { |
| 5590 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5591 | { |
| 5592 | // previous "if" or "elseif" jumps here |
| 5593 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5594 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5595 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | if (cctx->ctx_skip != MAYBE) |
| 5600 | cctx->ctx_skip = !cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5601 | |
| 5602 | return p; |
| 5603 | } |
| 5604 | |
| 5605 | static char_u * |
| 5606 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5607 | { |
| 5608 | scope_T *scope = cctx->ctx_scope; |
| 5609 | ifscope_T *ifscope; |
| 5610 | garray_T *instr = &cctx->ctx_instr; |
| 5611 | isn_T *isn; |
| 5612 | |
| 5613 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5614 | { |
| 5615 | emsg(_(e_endif_without_if)); |
| 5616 | return NULL; |
| 5617 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5618 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5619 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5620 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5621 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5622 | { |
| 5623 | // previous "if" or "elseif" jumps here |
| 5624 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5625 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5626 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5627 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5628 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5629 | cctx->ctx_skip = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5630 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5631 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5632 | return arg; |
| 5633 | } |
| 5634 | |
| 5635 | /* |
| 5636 | * compile "for var in expr" |
| 5637 | * |
| 5638 | * Produces instructions: |
| 5639 | * PUSHNR -1 |
| 5640 | * STORE loop-idx Set index to -1 |
| 5641 | * EVAL expr Push result of "expr" |
| 5642 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5643 | * - if beyond end, jump to "end" |
| 5644 | * - otherwise get item from list and push it |
| 5645 | * STORE var Store item in "var" |
| 5646 | * ... body ... |
| 5647 | * JUMP top Jump back to repeat |
| 5648 | * end: DROP Drop the result of "expr" |
| 5649 | * |
| 5650 | */ |
| 5651 | static char_u * |
| 5652 | compile_for(char_u *arg, cctx_T *cctx) |
| 5653 | { |
| 5654 | char_u *p; |
| 5655 | size_t varlen; |
| 5656 | garray_T *instr = &cctx->ctx_instr; |
| 5657 | garray_T *stack = &cctx->ctx_type_stack; |
| 5658 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5659 | lvar_T *loop_lvar; // loop iteration variable |
| 5660 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5661 | type_T *vartype; |
| 5662 | |
| 5663 | // TODO: list of variables: "for [key, value] in dict" |
| 5664 | // parse "var" |
| 5665 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5666 | ; |
| 5667 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5668 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5669 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5670 | { |
| 5671 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5672 | return NULL; |
| 5673 | } |
| 5674 | |
| 5675 | // consume "in" |
| 5676 | p = skipwhite(p); |
| 5677 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5678 | { |
| 5679 | emsg(_(e_missing_in)); |
| 5680 | return NULL; |
| 5681 | } |
| 5682 | p = skipwhite(p + 2); |
| 5683 | |
| 5684 | |
| 5685 | scope = new_scope(cctx, FOR_SCOPE); |
| 5686 | if (scope == NULL) |
| 5687 | return NULL; |
| 5688 | |
| 5689 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5690 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5691 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5692 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5693 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5694 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5695 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5696 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5697 | |
| 5698 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5699 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5700 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5701 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5702 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5703 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5704 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5705 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5706 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5707 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5708 | |
| 5709 | // compile "expr", it remains on the stack until "endfor" |
| 5710 | arg = p; |
| 5711 | if (compile_expr1(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5712 | { |
| 5713 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5714 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5715 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5716 | |
| 5717 | // now we know the type of "var" |
| 5718 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5719 | if (vartype->tt_type != VAR_LIST) |
| 5720 | { |
| 5721 | emsg(_("E1024: need a List to iterate over")); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5722 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5723 | return NULL; |
| 5724 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5725 | if (vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5726 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5727 | |
| 5728 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5729 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5730 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5731 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5732 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5733 | |
| 5734 | return arg; |
| 5735 | } |
| 5736 | |
| 5737 | /* |
| 5738 | * compile "endfor" |
| 5739 | */ |
| 5740 | static char_u * |
| 5741 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5742 | { |
| 5743 | garray_T *instr = &cctx->ctx_instr; |
| 5744 | scope_T *scope = cctx->ctx_scope; |
| 5745 | forscope_T *forscope; |
| 5746 | isn_T *isn; |
| 5747 | |
| 5748 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5749 | { |
| 5750 | emsg(_(e_for)); |
| 5751 | return NULL; |
| 5752 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5753 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5754 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5755 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5756 | |
| 5757 | // At end of ":for" scope jump back to the FOR instruction. |
| 5758 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5759 | |
| 5760 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5761 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5762 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5763 | |
| 5764 | // Fill in the "end" label any BREAK statements |
| 5765 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5766 | |
| 5767 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5768 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5769 | return NULL; |
| 5770 | |
| 5771 | vim_free(scope); |
| 5772 | |
| 5773 | return arg; |
| 5774 | } |
| 5775 | |
| 5776 | /* |
| 5777 | * compile "while expr" |
| 5778 | * |
| 5779 | * Produces instructions: |
| 5780 | * top: EVAL expr Push result of "expr" |
| 5781 | * JUMP_IF_FALSE end jump if false |
| 5782 | * ... body ... |
| 5783 | * JUMP top Jump back to repeat |
| 5784 | * end: |
| 5785 | * |
| 5786 | */ |
| 5787 | static char_u * |
| 5788 | compile_while(char_u *arg, cctx_T *cctx) |
| 5789 | { |
| 5790 | char_u *p = arg; |
| 5791 | garray_T *instr = &cctx->ctx_instr; |
| 5792 | scope_T *scope; |
| 5793 | |
| 5794 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5795 | if (scope == NULL) |
| 5796 | return NULL; |
| 5797 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5798 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5799 | |
| 5800 | // compile "expr" |
| 5801 | if (compile_expr1(&p, cctx) == FAIL) |
| 5802 | return NULL; |
| 5803 | |
| 5804 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5805 | 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] | 5806 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5807 | return FAIL; |
| 5808 | |
| 5809 | return p; |
| 5810 | } |
| 5811 | |
| 5812 | /* |
| 5813 | * compile "endwhile" |
| 5814 | */ |
| 5815 | static char_u * |
| 5816 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5817 | { |
| 5818 | scope_T *scope = cctx->ctx_scope; |
| 5819 | |
| 5820 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5821 | { |
| 5822 | emsg(_(e_while)); |
| 5823 | return NULL; |
| 5824 | } |
| 5825 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5826 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5827 | |
| 5828 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5829 | 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] | 5830 | |
| 5831 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5832 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5833 | 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] | 5834 | |
| 5835 | vim_free(scope); |
| 5836 | |
| 5837 | return arg; |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * compile "continue" |
| 5842 | */ |
| 5843 | static char_u * |
| 5844 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5845 | { |
| 5846 | scope_T *scope = cctx->ctx_scope; |
| 5847 | |
| 5848 | for (;;) |
| 5849 | { |
| 5850 | if (scope == NULL) |
| 5851 | { |
| 5852 | emsg(_(e_continue)); |
| 5853 | return NULL; |
| 5854 | } |
| 5855 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5856 | break; |
| 5857 | scope = scope->se_outer; |
| 5858 | } |
| 5859 | |
| 5860 | // Jump back to the FOR or WHILE instruction. |
| 5861 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5862 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5863 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5864 | return arg; |
| 5865 | } |
| 5866 | |
| 5867 | /* |
| 5868 | * compile "break" |
| 5869 | */ |
| 5870 | static char_u * |
| 5871 | compile_break(char_u *arg, cctx_T *cctx) |
| 5872 | { |
| 5873 | scope_T *scope = cctx->ctx_scope; |
| 5874 | endlabel_T **el; |
| 5875 | |
| 5876 | for (;;) |
| 5877 | { |
| 5878 | if (scope == NULL) |
| 5879 | { |
| 5880 | emsg(_(e_break)); |
| 5881 | return NULL; |
| 5882 | } |
| 5883 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5884 | break; |
| 5885 | scope = scope->se_outer; |
| 5886 | } |
| 5887 | |
| 5888 | // Jump to the end of the FOR or WHILE loop. |
| 5889 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5890 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5891 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5892 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5893 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5894 | return FAIL; |
| 5895 | |
| 5896 | return arg; |
| 5897 | } |
| 5898 | |
| 5899 | /* |
| 5900 | * compile "{" start of block |
| 5901 | */ |
| 5902 | static char_u * |
| 5903 | compile_block(char_u *arg, cctx_T *cctx) |
| 5904 | { |
| 5905 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5906 | return NULL; |
| 5907 | return skipwhite(arg + 1); |
| 5908 | } |
| 5909 | |
| 5910 | /* |
| 5911 | * compile end of block: drop one scope |
| 5912 | */ |
| 5913 | static void |
| 5914 | compile_endblock(cctx_T *cctx) |
| 5915 | { |
| 5916 | scope_T *scope = cctx->ctx_scope; |
| 5917 | |
| 5918 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5919 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5920 | vim_free(scope); |
| 5921 | } |
| 5922 | |
| 5923 | /* |
| 5924 | * compile "try" |
| 5925 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5926 | * finally. |
| 5927 | * Creates another scope for the "try" block itself. |
| 5928 | * TRY instruction sets up exception handling at runtime. |
| 5929 | * |
| 5930 | * "try" |
| 5931 | * TRY -> catch1, -> finally push trystack entry |
| 5932 | * ... try block |
| 5933 | * "throw {exception}" |
| 5934 | * EVAL {exception} |
| 5935 | * THROW create exception |
| 5936 | * ... try block |
| 5937 | * " catch {expr}" |
| 5938 | * JUMP -> finally |
| 5939 | * catch1: PUSH exeception |
| 5940 | * EVAL {expr} |
| 5941 | * MATCH |
| 5942 | * JUMP nomatch -> catch2 |
| 5943 | * CATCH remove exception |
| 5944 | * ... catch block |
| 5945 | * " catch" |
| 5946 | * JUMP -> finally |
| 5947 | * catch2: CATCH remove exception |
| 5948 | * ... catch block |
| 5949 | * " finally" |
| 5950 | * finally: |
| 5951 | * ... finally block |
| 5952 | * " endtry" |
| 5953 | * ENDTRY pop trystack entry, may rethrow |
| 5954 | */ |
| 5955 | static char_u * |
| 5956 | compile_try(char_u *arg, cctx_T *cctx) |
| 5957 | { |
| 5958 | garray_T *instr = &cctx->ctx_instr; |
| 5959 | scope_T *try_scope; |
| 5960 | scope_T *scope; |
| 5961 | |
| 5962 | // scope that holds the jumps that go to catch/finally/endtry |
| 5963 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5964 | if (try_scope == NULL) |
| 5965 | return NULL; |
| 5966 | |
| 5967 | // "catch" is set when the first ":catch" is found. |
| 5968 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5969 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5970 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5971 | return NULL; |
| 5972 | |
| 5973 | // scope for the try block itself |
| 5974 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5975 | if (scope == NULL) |
| 5976 | return NULL; |
| 5977 | |
| 5978 | return arg; |
| 5979 | } |
| 5980 | |
| 5981 | /* |
| 5982 | * compile "catch {expr}" |
| 5983 | */ |
| 5984 | static char_u * |
| 5985 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5986 | { |
| 5987 | scope_T *scope = cctx->ctx_scope; |
| 5988 | garray_T *instr = &cctx->ctx_instr; |
| 5989 | char_u *p; |
| 5990 | isn_T *isn; |
| 5991 | |
| 5992 | // end block scope from :try or :catch |
| 5993 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5994 | compile_endblock(cctx); |
| 5995 | scope = cctx->ctx_scope; |
| 5996 | |
| 5997 | // Error if not in a :try scope |
| 5998 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5999 | { |
| 6000 | emsg(_(e_catch)); |
| 6001 | return NULL; |
| 6002 | } |
| 6003 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6004 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6005 | { |
| 6006 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6007 | return NULL; |
| 6008 | } |
| 6009 | |
| 6010 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6011 | 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] | 6012 | JUMP_ALWAYS, cctx) == FAIL) |
| 6013 | return NULL; |
| 6014 | |
| 6015 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6016 | 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] | 6017 | if (isn->isn_arg.try.try_catch == 0) |
| 6018 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6019 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6020 | { |
| 6021 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6022 | 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] | 6023 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6024 | } |
| 6025 | |
| 6026 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6027 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6028 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6029 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6030 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6031 | } |
| 6032 | else |
| 6033 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6034 | char_u *end; |
| 6035 | char_u *pat; |
| 6036 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6037 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6038 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6039 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6040 | // Push v:exception, push {expr} and MATCH |
| 6041 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6042 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6043 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6044 | if (*end != *p) |
| 6045 | { |
| 6046 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6047 | vim_free(tofree); |
| 6048 | return FAIL; |
| 6049 | } |
| 6050 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6051 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6052 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6053 | len = (int)(end - tofree); |
| 6054 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6055 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6056 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6057 | if (pat == NULL) |
| 6058 | return FAIL; |
| 6059 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6060 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6061 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6062 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6063 | return NULL; |
| 6064 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6065 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6066 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6067 | return NULL; |
| 6068 | } |
| 6069 | |
| 6070 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6071 | return NULL; |
| 6072 | |
| 6073 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6074 | return NULL; |
| 6075 | return p; |
| 6076 | } |
| 6077 | |
| 6078 | static char_u * |
| 6079 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6080 | { |
| 6081 | scope_T *scope = cctx->ctx_scope; |
| 6082 | garray_T *instr = &cctx->ctx_instr; |
| 6083 | isn_T *isn; |
| 6084 | |
| 6085 | // end block scope from :try or :catch |
| 6086 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6087 | compile_endblock(cctx); |
| 6088 | scope = cctx->ctx_scope; |
| 6089 | |
| 6090 | // Error if not in a :try scope |
| 6091 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6092 | { |
| 6093 | emsg(_(e_finally)); |
| 6094 | return NULL; |
| 6095 | } |
| 6096 | |
| 6097 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6098 | 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] | 6099 | if (isn->isn_arg.try.try_finally != 0) |
| 6100 | { |
| 6101 | emsg(_(e_finally_dup)); |
| 6102 | return NULL; |
| 6103 | } |
| 6104 | |
| 6105 | // 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] | 6106 | 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] | 6107 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6108 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6109 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6110 | { |
| 6111 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6112 | 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] | 6113 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6114 | } |
| 6115 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6116 | // TODO: set index in ts_finally_label jumps |
| 6117 | |
| 6118 | return arg; |
| 6119 | } |
| 6120 | |
| 6121 | static char_u * |
| 6122 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6123 | { |
| 6124 | scope_T *scope = cctx->ctx_scope; |
| 6125 | garray_T *instr = &cctx->ctx_instr; |
| 6126 | isn_T *isn; |
| 6127 | |
| 6128 | // end block scope from :catch or :finally |
| 6129 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6130 | compile_endblock(cctx); |
| 6131 | scope = cctx->ctx_scope; |
| 6132 | |
| 6133 | // Error if not in a :try scope |
| 6134 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6135 | { |
| 6136 | if (scope == NULL) |
| 6137 | emsg(_(e_no_endtry)); |
| 6138 | else if (scope->se_type == WHILE_SCOPE) |
| 6139 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6140 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6141 | emsg(_(e_endfor)); |
| 6142 | else |
| 6143 | emsg(_(e_endif)); |
| 6144 | return NULL; |
| 6145 | } |
| 6146 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6147 | 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] | 6148 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6149 | { |
| 6150 | emsg(_("E1032: missing :catch or :finally")); |
| 6151 | return NULL; |
| 6152 | } |
| 6153 | |
| 6154 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6155 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6156 | 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] | 6157 | |
| 6158 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 6159 | if (isn->isn_arg.try.try_finally == 0) |
| 6160 | isn->isn_arg.try.try_finally = instr->ga_len; |
| 6161 | compile_endblock(cctx); |
| 6162 | |
| 6163 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6164 | return NULL; |
| 6165 | return arg; |
| 6166 | } |
| 6167 | |
| 6168 | /* |
| 6169 | * compile "throw {expr}" |
| 6170 | */ |
| 6171 | static char_u * |
| 6172 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6173 | { |
| 6174 | char_u *p = skipwhite(arg); |
| 6175 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6176 | if (compile_expr1(&p, cctx) == FAIL) |
| 6177 | return NULL; |
| 6178 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6179 | return NULL; |
| 6180 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6181 | return NULL; |
| 6182 | |
| 6183 | return p; |
| 6184 | } |
| 6185 | |
| 6186 | /* |
| 6187 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6188 | * compile "echomsg expr" |
| 6189 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6190 | * compile "execute expr" |
| 6191 | */ |
| 6192 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6193 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6194 | { |
| 6195 | char_u *p = arg; |
| 6196 | int count = 0; |
| 6197 | |
| 6198 | for (;;) |
| 6199 | { |
| 6200 | if (compile_expr1(&p, cctx) == FAIL) |
| 6201 | return NULL; |
| 6202 | ++count; |
| 6203 | p = skipwhite(p); |
| 6204 | if (ends_excmd(*p)) |
| 6205 | break; |
| 6206 | } |
| 6207 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6208 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6209 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6210 | else if (cmdidx == CMD_execute) |
| 6211 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6212 | else if (cmdidx == CMD_echomsg) |
| 6213 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6214 | else |
| 6215 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6216 | return p; |
| 6217 | } |
| 6218 | |
| 6219 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6220 | * A command that is not compiled, execute with legacy code. |
| 6221 | */ |
| 6222 | static char_u * |
| 6223 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6224 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6225 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6226 | int has_expr = FALSE; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6227 | |
| 6228 | if (cctx->ctx_skip == TRUE) |
| 6229 | goto theend; |
| 6230 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6231 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
| 6232 | has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND)); |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6233 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6234 | { |
| 6235 | // expand filename in "syntax include [@group] filename" |
| 6236 | has_expr = TRUE; |
| 6237 | eap->arg = skipwhite(eap->arg + 7); |
| 6238 | if (*eap->arg == '@') |
| 6239 | eap->arg = skiptowhite(eap->arg); |
| 6240 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6241 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6242 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6243 | { |
| 6244 | int count = 0; |
| 6245 | char_u *start = skipwhite(line); |
| 6246 | |
| 6247 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6248 | // PUSHS ":cmd xxx" |
| 6249 | // eval expr1 |
| 6250 | // PUSHS "yyy" |
| 6251 | // eval expr2 |
| 6252 | // PUSHS "zzz" |
| 6253 | // EXECCONCAT 5 |
| 6254 | for (;;) |
| 6255 | { |
| 6256 | if (p > start) |
| 6257 | { |
| 6258 | generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start))); |
| 6259 | ++count; |
| 6260 | } |
| 6261 | p += 2; |
| 6262 | if (compile_expr1(&p, cctx) == FAIL) |
| 6263 | return NULL; |
| 6264 | may_generate_2STRING(-1, cctx); |
| 6265 | ++count; |
| 6266 | p = skipwhite(p); |
| 6267 | if (*p != '`') |
| 6268 | { |
| 6269 | emsg(_("E1083: missing backtick")); |
| 6270 | return NULL; |
| 6271 | } |
| 6272 | start = p + 1; |
| 6273 | |
| 6274 | p = (char_u *)strstr((char *)start, "`="); |
| 6275 | if (p == NULL) |
| 6276 | { |
| 6277 | if (*skipwhite(start) != NUL) |
| 6278 | { |
| 6279 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6280 | ++count; |
| 6281 | } |
| 6282 | break; |
| 6283 | } |
| 6284 | } |
| 6285 | generate_EXECCONCAT(cctx, count); |
| 6286 | } |
| 6287 | else |
| 6288 | generate_EXEC(cctx, line); |
| 6289 | |
| 6290 | theend: |
| 6291 | return (char_u *)""; |
| 6292 | } |
| 6293 | |
| 6294 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6295 | * After ex_function() has collected all the function lines: parse and compile |
| 6296 | * the lines into instructions. |
| 6297 | * Adds the function to "def_functions". |
| 6298 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6299 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6300 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6301 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6302 | * "def_functions". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6303 | */ |
| 6304 | void |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6305 | 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] | 6306 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6307 | char_u *line = NULL; |
| 6308 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6309 | char *errormsg = NULL; // error message |
| 6310 | int had_return = FALSE; |
| 6311 | cctx_T cctx; |
| 6312 | garray_T *instr; |
| 6313 | int called_emsg_before = called_emsg; |
| 6314 | int ret = FAIL; |
| 6315 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6316 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6317 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6318 | { |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6319 | dfunc_T *dfunc; // may be invalidated by compile_lambda() |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6320 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6321 | if (ufunc->uf_dfunc_idx >= 0) |
| 6322 | { |
| 6323 | // Redefining a function that was compiled before. |
| 6324 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
| 6325 | |
| 6326 | // Free old instructions. |
| 6327 | delete_def_function_contents(dfunc); |
| 6328 | } |
| 6329 | else |
| 6330 | { |
| 6331 | // Add the function to "def_functions". |
| 6332 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6333 | return; |
| 6334 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6335 | CLEAR_POINTER(dfunc); |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6336 | dfunc->df_idx = def_functions.ga_len; |
| 6337 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6338 | dfunc->df_ufunc = ufunc; |
| 6339 | ++def_functions.ga_len; |
| 6340 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6341 | } |
| 6342 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6343 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6344 | cctx.ctx_ufunc = ufunc; |
| 6345 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6346 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6347 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6348 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6349 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6350 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6351 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6352 | instr = &cctx.ctx_instr; |
| 6353 | |
| 6354 | // Most modern script version. |
| 6355 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6356 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6357 | if (ufunc->uf_def_args.ga_len > 0) |
| 6358 | { |
| 6359 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6360 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6361 | int i; |
| 6362 | char_u *arg; |
| 6363 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6364 | |
| 6365 | // Produce instructions for the default values of optional arguments. |
| 6366 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6367 | // where to start when the function is called, depending on the number |
| 6368 | // of arguments. |
| 6369 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6370 | if (ufunc->uf_def_arg_idx == NULL) |
| 6371 | goto erret; |
| 6372 | for (i = 0; i < count; ++i) |
| 6373 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6374 | garray_T *stack = &cctx.ctx_type_stack; |
| 6375 | type_T *val_type; |
| 6376 | int arg_idx = first_def_arg + i; |
| 6377 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6378 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6379 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6380 | if (compile_expr1(&arg, &cctx) == FAIL) |
| 6381 | goto erret; |
| 6382 | |
| 6383 | // If no type specified use the type of the default value. |
| 6384 | // Otherwise check that the default value type matches the |
| 6385 | // specified type. |
| 6386 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6387 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6388 | ufunc->uf_arg_types[arg_idx] = val_type; |
| 6389 | else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE) |
| 6390 | == FAIL) |
| 6391 | { |
| 6392 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6393 | arg_idx + 1); |
| 6394 | goto erret; |
| 6395 | } |
| 6396 | |
| 6397 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6398 | goto erret; |
| 6399 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6400 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6401 | } |
| 6402 | |
| 6403 | /* |
| 6404 | * Loop over all the lines of the function and generate instructions. |
| 6405 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6406 | for (;;) |
| 6407 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6408 | exarg_T ea; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6409 | int is_ex_command = FALSE; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6410 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6411 | // Bail out on the first error to avoid a flood of errors and report |
| 6412 | // the right line number when inside try/catch. |
| 6413 | if (emsg_before != called_emsg) |
| 6414 | goto erret; |
| 6415 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6416 | if (line != NULL && *line == '|') |
| 6417 | // the line continues after a '|' |
| 6418 | ++line; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6419 | else if (line != NULL && *line != NUL |
| 6420 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6421 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6422 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6423 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6424 | goto erret; |
| 6425 | } |
| 6426 | else |
| 6427 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6428 | line = next_line_from_context(&cctx); |
| 6429 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6430 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6431 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6432 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6433 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6434 | |
| 6435 | had_return = FALSE; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6436 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6437 | ea.cmdlinep = &line; |
| 6438 | ea.cmd = skipwhite(line); |
| 6439 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6440 | // Some things can be recognized by the first character. |
| 6441 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6442 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6443 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6444 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6445 | if (ea.cmd[1] != '{') |
| 6446 | { |
| 6447 | line = (char_u *)""; |
| 6448 | continue; |
| 6449 | } |
| 6450 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6451 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6452 | case '}': |
| 6453 | { |
| 6454 | // "}" ends a block scope |
| 6455 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6456 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6457 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6458 | if (stype == BLOCK_SCOPE) |
| 6459 | { |
| 6460 | compile_endblock(&cctx); |
| 6461 | line = ea.cmd; |
| 6462 | } |
| 6463 | else |
| 6464 | { |
| 6465 | emsg(_("E1025: using } outside of a block scope")); |
| 6466 | goto erret; |
| 6467 | } |
| 6468 | if (line != NULL) |
| 6469 | line = skipwhite(ea.cmd + 1); |
| 6470 | continue; |
| 6471 | } |
| 6472 | |
| 6473 | case '{': |
| 6474 | // "{" starts a block scope |
| 6475 | // "{'a': 1}->func() is something else |
| 6476 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6477 | { |
| 6478 | line = compile_block(ea.cmd, &cctx); |
| 6479 | continue; |
| 6480 | } |
| 6481 | break; |
| 6482 | |
| 6483 | case ':': |
| 6484 | is_ex_command = TRUE; |
| 6485 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6486 | } |
| 6487 | |
| 6488 | /* |
| 6489 | * COMMAND MODIFIERS |
| 6490 | */ |
| 6491 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6492 | { |
| 6493 | if (errormsg != NULL) |
| 6494 | goto erret; |
| 6495 | // empty line or comment |
| 6496 | line = (char_u *)""; |
| 6497 | continue; |
| 6498 | } |
| 6499 | |
| 6500 | // Skip ":call" to get to the function name. |
| 6501 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 6502 | ea.cmd = skipwhite(ea.cmd); |
| 6503 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6504 | if (!is_ex_command) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6505 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6506 | // Assuming the command starts with a variable or function name, |
| 6507 | // find what follows. Also "&opt = val", "$ENV = val" and "@r = |
| 6508 | // val". |
| 6509 | p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
| 6510 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6511 | p = to_name_end(p, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6512 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6513 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6514 | int oplen; |
| 6515 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6516 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6517 | oplen = assignment_len(skipwhite(p), &heredoc); |
| 6518 | if (oplen > 0) |
| 6519 | { |
| 6520 | // Recognize an assignment if we recognize the variable |
| 6521 | // name: |
| 6522 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6523 | // "local = expr" where "local" is a local var. |
| 6524 | // "script = expr" where "script" is a script-local var. |
| 6525 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6526 | // "&opt = expr" |
| 6527 | // "$ENV = expr" |
| 6528 | // "@r = expr" |
| 6529 | if (*ea.cmd == '&' |
| 6530 | || *ea.cmd == '$' |
| 6531 | || *ea.cmd == '@' |
| 6532 | || ((p - ea.cmd) > 2 && ea.cmd[1] == ':') |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6533 | || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6534 | || lookup_script(ea.cmd, p - ea.cmd) == OK |
| 6535 | || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL) |
| 6536 | { |
| 6537 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6538 | if (line == NULL) |
| 6539 | goto erret; |
| 6540 | continue; |
| 6541 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6542 | } |
| 6543 | } |
| 6544 | } |
| 6545 | |
| 6546 | /* |
| 6547 | * COMMAND after range |
| 6548 | */ |
| 6549 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6550 | p = find_ex_command(&ea, NULL, is_ex_command ? NULL |
| 6551 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6552 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6553 | |
| 6554 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6555 | { |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6556 | if (cctx.ctx_skip == TRUE) |
| 6557 | { |
| 6558 | line += STRLEN(line); |
| 6559 | continue; |
| 6560 | } |
| 6561 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6562 | // Expression or function call. |
| 6563 | if (ea.cmdidx == CMD_eval) |
| 6564 | { |
| 6565 | p = ea.cmd; |
| 6566 | if (compile_expr1(&p, &cctx) == FAIL) |
| 6567 | goto erret; |
| 6568 | |
| 6569 | // drop the return value |
| 6570 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6571 | line = p; |
| 6572 | continue; |
| 6573 | } |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6574 | // CMD_let cannot happen, compile_assignment() above is used |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6575 | iemsg("Command from find_ex_command() not handled"); |
| 6576 | goto erret; |
| 6577 | } |
| 6578 | |
| 6579 | p = skipwhite(p); |
| 6580 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6581 | if (cctx.ctx_skip == TRUE |
| 6582 | && ea.cmdidx != CMD_elseif |
| 6583 | && ea.cmdidx != CMD_else |
| 6584 | && ea.cmdidx != CMD_endif) |
| 6585 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6586 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6587 | continue; |
| 6588 | } |
| 6589 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6590 | switch (ea.cmdidx) |
| 6591 | { |
| 6592 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6593 | ea.arg = p; |
| 6594 | line = compile_nested_function(&ea, &cctx); |
| 6595 | break; |
| 6596 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6597 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6598 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6599 | goto erret; |
| 6600 | |
| 6601 | case CMD_return: |
| 6602 | line = compile_return(p, set_return_type, &cctx); |
| 6603 | had_return = TRUE; |
| 6604 | break; |
| 6605 | |
| 6606 | case CMD_let: |
| 6607 | case CMD_const: |
| 6608 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
| 6609 | break; |
| 6610 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6611 | case CMD_unlet: |
| 6612 | case CMD_unlockvar: |
| 6613 | case CMD_lockvar: |
| 6614 | line = compile_unletlock(p, &ea, &cctx); |
| 6615 | break; |
| 6616 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6617 | case CMD_import: |
| 6618 | line = compile_import(p, &cctx); |
| 6619 | break; |
| 6620 | |
| 6621 | case CMD_if: |
| 6622 | line = compile_if(p, &cctx); |
| 6623 | break; |
| 6624 | case CMD_elseif: |
| 6625 | line = compile_elseif(p, &cctx); |
| 6626 | break; |
| 6627 | case CMD_else: |
| 6628 | line = compile_else(p, &cctx); |
| 6629 | break; |
| 6630 | case CMD_endif: |
| 6631 | line = compile_endif(p, &cctx); |
| 6632 | break; |
| 6633 | |
| 6634 | case CMD_while: |
| 6635 | line = compile_while(p, &cctx); |
| 6636 | break; |
| 6637 | case CMD_endwhile: |
| 6638 | line = compile_endwhile(p, &cctx); |
| 6639 | break; |
| 6640 | |
| 6641 | case CMD_for: |
| 6642 | line = compile_for(p, &cctx); |
| 6643 | break; |
| 6644 | case CMD_endfor: |
| 6645 | line = compile_endfor(p, &cctx); |
| 6646 | break; |
| 6647 | case CMD_continue: |
| 6648 | line = compile_continue(p, &cctx); |
| 6649 | break; |
| 6650 | case CMD_break: |
| 6651 | line = compile_break(p, &cctx); |
| 6652 | break; |
| 6653 | |
| 6654 | case CMD_try: |
| 6655 | line = compile_try(p, &cctx); |
| 6656 | break; |
| 6657 | case CMD_catch: |
| 6658 | line = compile_catch(p, &cctx); |
| 6659 | break; |
| 6660 | case CMD_finally: |
| 6661 | line = compile_finally(p, &cctx); |
| 6662 | break; |
| 6663 | case CMD_endtry: |
| 6664 | line = compile_endtry(p, &cctx); |
| 6665 | break; |
| 6666 | case CMD_throw: |
| 6667 | line = compile_throw(p, &cctx); |
| 6668 | break; |
| 6669 | |
| 6670 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6671 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6672 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6673 | case CMD_echomsg: |
| 6674 | case CMD_echoerr: |
| 6675 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6676 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6677 | |
| 6678 | default: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6679 | // TODO: other commands with an expression argument |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6680 | // Not recognized, execute with do_cmdline_cmd(). |
| 6681 | ea.arg = p; |
| 6682 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6683 | break; |
| 6684 | } |
| 6685 | if (line == NULL) |
| 6686 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6687 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6688 | |
| 6689 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6690 | { |
| 6691 | iemsg("Type stack underflow"); |
| 6692 | goto erret; |
| 6693 | } |
| 6694 | } |
| 6695 | |
| 6696 | if (cctx.ctx_scope != NULL) |
| 6697 | { |
| 6698 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6699 | emsg(_(e_endif)); |
| 6700 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6701 | emsg(_(e_endwhile)); |
| 6702 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6703 | emsg(_(e_endfor)); |
| 6704 | else |
| 6705 | emsg(_("E1026: Missing }")); |
| 6706 | goto erret; |
| 6707 | } |
| 6708 | |
| 6709 | if (!had_return) |
| 6710 | { |
| 6711 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6712 | { |
| 6713 | emsg(_("E1027: Missing return statement")); |
| 6714 | goto erret; |
| 6715 | } |
| 6716 | |
| 6717 | // Return zero if there is no return at the end. |
| 6718 | generate_PUSHNR(&cctx, 0); |
| 6719 | generate_instr(&cctx, ISN_RETURN); |
| 6720 | } |
| 6721 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6722 | { |
| 6723 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6724 | + ufunc->uf_dfunc_idx; |
| 6725 | dfunc->df_deleted = FALSE; |
| 6726 | dfunc->df_instr = instr->ga_data; |
| 6727 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6728 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6729 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6730 | if (cctx.ctx_outer_used) |
| 6731 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6732 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6733 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6734 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6735 | int varargs = ufunc->uf_va_name != NULL; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6736 | int argcount = ufunc->uf_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6737 | |
| 6738 | // Create a type for the function, with the return type and any |
| 6739 | // argument types. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6740 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6741 | // The type is included in "tt_args". |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6742 | if (argcount > 0 || varargs) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6743 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6744 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6745 | argcount, &ufunc->uf_type_list); |
| 6746 | // Add argument types to the function type. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6747 | if (func_type_add_arg_types(ufunc->uf_func_type, |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6748 | argcount + varargs, |
| 6749 | &ufunc->uf_type_list) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6750 | { |
| 6751 | ret = FAIL; |
| 6752 | goto erret; |
| 6753 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6754 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6755 | ufunc->uf_func_type->tt_min_argcount = |
| 6756 | argcount - ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6757 | if (ufunc->uf_arg_types == NULL) |
| 6758 | { |
| 6759 | int i; |
| 6760 | |
| 6761 | // lambda does not have argument types. |
| 6762 | for (i = 0; i < argcount; ++i) |
| 6763 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6764 | } |
| 6765 | else |
| 6766 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6767 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6768 | if (varargs) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6769 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6770 | ufunc->uf_func_type->tt_args[argcount] = |
| 6771 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6772 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6773 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6774 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6775 | else |
| 6776 | // No arguments, can use a predefined type. |
| 6777 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6778 | argcount, &ufunc->uf_type_list); |
| 6779 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6780 | } |
| 6781 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6782 | ret = OK; |
| 6783 | |
| 6784 | erret: |
| 6785 | if (ret == FAIL) |
| 6786 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6787 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6788 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6789 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6790 | |
| 6791 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6792 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6793 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6794 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6795 | ufunc->uf_dfunc_idx = -1; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6796 | if (!dfunc->df_deleted) |
| 6797 | --def_functions.ga_len; |
| 6798 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6799 | while (cctx.ctx_scope != NULL) |
| 6800 | drop_scope(&cctx); |
| 6801 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6802 | // Don't execute this function body. |
| 6803 | ga_clear_strings(&ufunc->uf_lines); |
| 6804 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6805 | if (errormsg != NULL) |
| 6806 | emsg(errormsg); |
| 6807 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6808 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | current_sctx = save_current_sctx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6812 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6813 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6814 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | /* |
| 6818 | * Delete an instruction, free what it contains. |
| 6819 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6820 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6821 | delete_instr(isn_T *isn) |
| 6822 | { |
| 6823 | switch (isn->isn_type) |
| 6824 | { |
| 6825 | case ISN_EXEC: |
| 6826 | case ISN_LOADENV: |
| 6827 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6828 | case ISN_LOADB: |
| 6829 | case ISN_LOADW: |
| 6830 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6831 | case ISN_LOADOPT: |
| 6832 | case ISN_MEMBER: |
| 6833 | case ISN_PUSHEXC: |
| 6834 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6835 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6836 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6837 | case ISN_STOREB: |
| 6838 | case ISN_STOREW: |
| 6839 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6840 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6841 | vim_free(isn->isn_arg.string); |
| 6842 | break; |
| 6843 | |
| 6844 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6845 | case ISN_STORES: |
| 6846 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6847 | break; |
| 6848 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6849 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 6850 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6851 | vim_free(isn->isn_arg.unlet.ul_name); |
| 6852 | break; |
| 6853 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6854 | case ISN_STOREOPT: |
| 6855 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6856 | break; |
| 6857 | |
| 6858 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6859 | blob_unref(isn->isn_arg.blob); |
| 6860 | break; |
| 6861 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6862 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6863 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6864 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6865 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6866 | break; |
| 6867 | |
| 6868 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6869 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6870 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6871 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6872 | break; |
| 6873 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6874 | case ISN_UCALL: |
| 6875 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6876 | break; |
| 6877 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 6878 | case ISN_FUNCREF: |
| 6879 | { |
| 6880 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6881 | + isn->isn_arg.funcref.fr_func; |
| 6882 | func_ptr_unref(dfunc->df_ufunc); |
| 6883 | } |
| 6884 | break; |
| 6885 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6886 | case ISN_2BOOL: |
| 6887 | case ISN_2STRING: |
| 6888 | case ISN_ADDBLOB: |
| 6889 | case ISN_ADDLIST: |
| 6890 | case ISN_BCALL: |
| 6891 | case ISN_CATCH: |
| 6892 | case ISN_CHECKNR: |
| 6893 | case ISN_CHECKTYPE: |
| 6894 | case ISN_COMPAREANY: |
| 6895 | case ISN_COMPAREBLOB: |
| 6896 | case ISN_COMPAREBOOL: |
| 6897 | case ISN_COMPAREDICT: |
| 6898 | case ISN_COMPAREFLOAT: |
| 6899 | case ISN_COMPAREFUNC: |
| 6900 | case ISN_COMPARELIST: |
| 6901 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6902 | case ISN_COMPARESPECIAL: |
| 6903 | case ISN_COMPARESTRING: |
| 6904 | case ISN_CONCAT: |
| 6905 | case ISN_DCALL: |
| 6906 | case ISN_DROP: |
| 6907 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6908 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6909 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6910 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6911 | case ISN_EXECCONCAT: |
| 6912 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6913 | case ISN_FOR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6914 | case ISN_INDEX: |
| 6915 | case ISN_JUMP: |
| 6916 | case ISN_LOAD: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6917 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6918 | case ISN_LOADSCRIPT: |
| 6919 | case ISN_LOADREG: |
| 6920 | case ISN_LOADV: |
| 6921 | case ISN_NEGATENR: |
| 6922 | case ISN_NEWDICT: |
| 6923 | case ISN_NEWLIST: |
| 6924 | case ISN_OPNR: |
| 6925 | case ISN_OPFLOAT: |
| 6926 | case ISN_OPANY: |
| 6927 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6928 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6929 | case ISN_PUSHF: |
| 6930 | case ISN_PUSHNR: |
| 6931 | case ISN_PUSHBOOL: |
| 6932 | case ISN_PUSHSPEC: |
| 6933 | case ISN_RETURN: |
| 6934 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 6935 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6936 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6937 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6938 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6939 | case ISN_STORESCRIPT: |
| 6940 | case ISN_THROW: |
| 6941 | case ISN_TRY: |
| 6942 | // nothing allocated |
| 6943 | break; |
| 6944 | } |
| 6945 | } |
| 6946 | |
| 6947 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6948 | * Free all instructions for "dfunc". |
| 6949 | */ |
| 6950 | static void |
| 6951 | delete_def_function_contents(dfunc_T *dfunc) |
| 6952 | { |
| 6953 | int idx; |
| 6954 | |
| 6955 | ga_clear(&dfunc->df_def_args_isn); |
| 6956 | |
| 6957 | if (dfunc->df_instr != NULL) |
| 6958 | { |
| 6959 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 6960 | delete_instr(dfunc->df_instr + idx); |
| 6961 | VIM_CLEAR(dfunc->df_instr); |
| 6962 | } |
| 6963 | |
| 6964 | dfunc->df_deleted = TRUE; |
| 6965 | } |
| 6966 | |
| 6967 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6968 | * When a user function is deleted, delete any associated def function. |
| 6969 | */ |
| 6970 | void |
| 6971 | delete_def_function(ufunc_T *ufunc) |
| 6972 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6973 | if (ufunc->uf_dfunc_idx >= 0) |
| 6974 | { |
| 6975 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6976 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6977 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6978 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6979 | } |
| 6980 | } |
| 6981 | |
| 6982 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6983 | /* |
| 6984 | * Free all functions defined with ":def". |
| 6985 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6986 | void |
| 6987 | free_def_functions(void) |
| 6988 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6989 | int idx; |
| 6990 | |
| 6991 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 6992 | { |
| 6993 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 6994 | |
| 6995 | delete_def_function_contents(dfunc); |
| 6996 | } |
| 6997 | |
| 6998 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6999 | } |
| 7000 | #endif |
| 7001 | |
| 7002 | |
| 7003 | #endif // FEAT_EVAL |