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