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