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"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 138 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 139 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 140 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 141 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
| 142 | 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] | 143 | |
| 144 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 145 | * Lookup variable "name" in the local scope and return it. |
| 146 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 147 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 148 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 149 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 150 | { |
| 151 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 152 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 153 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 154 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 155 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 156 | |
| 157 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 159 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 160 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 161 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 162 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | { |
| 164 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 165 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 166 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 167 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 168 | |
| 169 | // Find local in outer function scope. |
| 170 | if (cctx->ctx_outer != NULL) |
| 171 | { |
| 172 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 173 | if (lvar != NULL) |
| 174 | { |
| 175 | // TODO: are there situations we should not mark the outer scope as |
| 176 | // used? |
| 177 | cctx->ctx_outer_used = TRUE; |
| 178 | lvar->lv_from_outer = TRUE; |
| 179 | return lvar; |
| 180 | } |
| 181 | } |
| 182 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 183 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 187 | * Lookup an argument in the current function and an enclosing function. |
| 188 | * Returns the argument index in "idxp" |
| 189 | * Returns the argument type in "type" |
| 190 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 191 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 192 | */ |
| 193 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 194 | lookup_arg( |
| 195 | char_u *name, |
| 196 | size_t len, |
| 197 | int *idxp, |
| 198 | type_T **type, |
| 199 | int *gen_load_outer, |
| 200 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 201 | { |
| 202 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 203 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 204 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 205 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 206 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 207 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 208 | { |
| 209 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 210 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 211 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 212 | { |
| 213 | if (idxp != NULL) |
| 214 | { |
| 215 | // Arguments are located above the frame pointer. One further |
| 216 | // if there is a vararg argument |
| 217 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 218 | + STACK_FRAME_SIZE) |
| 219 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 220 | |
| 221 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 222 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 223 | else |
| 224 | *type = &t_any; |
| 225 | } |
| 226 | return OK; |
| 227 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 228 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 229 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 230 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 231 | if (va_name != NULL |
| 232 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 233 | { |
| 234 | if (idxp != NULL) |
| 235 | { |
| 236 | // varargs is always the last argument |
| 237 | *idxp = -STACK_FRAME_SIZE - 1; |
| 238 | *type = cctx->ctx_ufunc->uf_va_type; |
| 239 | } |
| 240 | return OK; |
| 241 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 242 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 243 | if (cctx->ctx_outer != NULL) |
| 244 | { |
| 245 | // Lookup the name for an argument of the outer function. |
| 246 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 247 | == OK) |
| 248 | { |
| 249 | *gen_load_outer = TRUE; |
| 250 | return OK; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Lookup a variable in the current script. |
| 259 | * Returns OK or FAIL. |
| 260 | */ |
| 261 | static int |
| 262 | lookup_script(char_u *name, size_t len) |
| 263 | { |
| 264 | int cc; |
| 265 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 266 | dictitem_T *di; |
| 267 | |
| 268 | cc = name[len]; |
| 269 | name[len] = NUL; |
| 270 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 271 | name[len] = cc; |
| 272 | return di == NULL ? FAIL: OK; |
| 273 | } |
| 274 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 275 | /* |
| 276 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 277 | * compilation context "cctx". |
| 278 | * Return FAIL and give an error if it defined. |
| 279 | */ |
| 280 | int |
| 281 | check_defined(char_u *p, int len, cctx_T *cctx) |
| 282 | { |
| 283 | if (lookup_script(p, len) == OK |
| 284 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 285 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | || find_imported(p, len, cctx) != NULL))) |
| 287 | { |
| 288 | semsg("E1073: imported name already defined: %s", p); |
| 289 | return FAIL; |
| 290 | } |
| 291 | return OK; |
| 292 | } |
| 293 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 296 | * be freed later. |
| 297 | */ |
| 298 | static type_T * |
| 299 | alloc_type(garray_T *type_gap) |
| 300 | { |
| 301 | type_T *type; |
| 302 | |
| 303 | if (ga_grow(type_gap, 1) == FAIL) |
| 304 | return NULL; |
| 305 | type = ALLOC_CLEAR_ONE(type_T); |
| 306 | if (type != NULL) |
| 307 | { |
| 308 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 309 | ++type_gap->ga_len; |
| 310 | } |
| 311 | return type; |
| 312 | } |
| 313 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 314 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 315 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 316 | { |
| 317 | type_T *type; |
| 318 | |
| 319 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 320 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 321 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 322 | if (member_type->tt_type == VAR_VOID |
| 323 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 324 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 325 | if (member_type->tt_type == VAR_BOOL) |
| 326 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 327 | if (member_type->tt_type == VAR_NUMBER) |
| 328 | return &t_list_number; |
| 329 | if (member_type->tt_type == VAR_STRING) |
| 330 | return &t_list_string; |
| 331 | |
| 332 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 333 | type = alloc_type(type_gap); |
| 334 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 335 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 336 | type->tt_type = VAR_LIST; |
| 337 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 338 | type->tt_argcount = 0; |
| 339 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | return type; |
| 341 | } |
| 342 | |
| 343 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 344 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 345 | { |
| 346 | type_T *type; |
| 347 | |
| 348 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 349 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 350 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 351 | if (member_type->tt_type == VAR_VOID |
| 352 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 353 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 354 | if (member_type->tt_type == VAR_BOOL) |
| 355 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 356 | if (member_type->tt_type == VAR_NUMBER) |
| 357 | return &t_dict_number; |
| 358 | if (member_type->tt_type == VAR_STRING) |
| 359 | return &t_dict_string; |
| 360 | |
| 361 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 362 | type = alloc_type(type_gap); |
| 363 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 364 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 365 | type->tt_type = VAR_DICT; |
| 366 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 367 | type->tt_argcount = 0; |
| 368 | type->tt_args = NULL; |
| 369 | return type; |
| 370 | } |
| 371 | |
| 372 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 373 | * Allocate a new type for a function. |
| 374 | */ |
| 375 | static type_T * |
| 376 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 377 | { |
| 378 | type_T *type = alloc_type(type_gap); |
| 379 | |
| 380 | if (type == NULL) |
| 381 | return &t_any; |
| 382 | type->tt_type = VAR_FUNC; |
| 383 | type->tt_member = ret_type; |
| 384 | type->tt_argcount = argcount; |
| 385 | type->tt_args = NULL; |
| 386 | return type; |
| 387 | } |
| 388 | |
| 389 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 390 | * Get a function type, based on the return type "ret_type". |
| 391 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 392 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 393 | */ |
| 394 | static type_T * |
| 395 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 396 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 397 | // recognize commonly used types |
| 398 | if (argcount <= 0) |
| 399 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 400 | if (ret_type == &t_unknown) |
| 401 | { |
| 402 | // (argcount == 0) is not possible |
| 403 | return &t_func_unknown; |
| 404 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 405 | if (ret_type == &t_void) |
| 406 | { |
| 407 | if (argcount == 0) |
| 408 | return &t_func_0_void; |
| 409 | else |
| 410 | return &t_func_void; |
| 411 | } |
| 412 | if (ret_type == &t_any) |
| 413 | { |
| 414 | if (argcount == 0) |
| 415 | return &t_func_0_any; |
| 416 | else |
| 417 | return &t_func_any; |
| 418 | } |
| 419 | if (ret_type == &t_number) |
| 420 | { |
| 421 | if (argcount == 0) |
| 422 | return &t_func_0_number; |
| 423 | else |
| 424 | return &t_func_number; |
| 425 | } |
| 426 | if (ret_type == &t_string) |
| 427 | { |
| 428 | if (argcount == 0) |
| 429 | return &t_func_0_string; |
| 430 | else |
| 431 | return &t_func_string; |
| 432 | } |
| 433 | } |
| 434 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 435 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 438 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 439 | * For a function type, reserve space for "argcount" argument types (including |
| 440 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 441 | */ |
| 442 | static int |
| 443 | func_type_add_arg_types( |
| 444 | type_T *functype, |
| 445 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 446 | garray_T *type_gap) |
| 447 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 448 | // To make it easy to free the space needed for the argument types, add the |
| 449 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 450 | if (ga_grow(type_gap, 1) == FAIL) |
| 451 | return FAIL; |
| 452 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 453 | if (functype->tt_args == NULL) |
| 454 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 455 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 456 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 457 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 458 | return OK; |
| 459 | } |
| 460 | |
| 461 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 462 | * Return the type_T for a typval. Only for primitive types. |
| 463 | */ |
| 464 | static type_T * |
| 465 | typval2type(typval_T *tv) |
| 466 | { |
| 467 | if (tv->v_type == VAR_NUMBER) |
| 468 | return &t_number; |
| 469 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 470 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 471 | if (tv->v_type == VAR_STRING) |
| 472 | return &t_string; |
| 473 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 474 | return &t_list_string; |
| 475 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 476 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 477 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 480 | static void |
| 481 | type_mismatch(type_T *expected, type_T *actual) |
| 482 | { |
| 483 | char *tofree1, *tofree2; |
| 484 | |
| 485 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 486 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 487 | vim_free(tofree1); |
| 488 | vim_free(tofree2); |
| 489 | } |
| 490 | |
| 491 | static void |
| 492 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 493 | { |
| 494 | char *tofree1, *tofree2; |
| 495 | |
| 496 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 497 | argidx, |
| 498 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 499 | vim_free(tofree1); |
| 500 | vim_free(tofree2); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Check if the expected and actual types match. |
| 505 | * Does not allow for assigning "any" to a specific type. |
| 506 | */ |
| 507 | static int |
| 508 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 509 | { |
| 510 | int ret = OK; |
| 511 | |
| 512 | // When expected is "unknown" we accept any actual type. |
| 513 | // When expected is "any" we accept any actual type except "void". |
| 514 | if (expected->tt_type != VAR_UNKNOWN |
| 515 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 516 | |
| 517 | { |
| 518 | if (expected->tt_type != actual->tt_type) |
| 519 | { |
| 520 | if (give_msg) |
| 521 | type_mismatch(expected, actual); |
| 522 | return FAIL; |
| 523 | } |
| 524 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 525 | { |
| 526 | // "unknown" is used for an empty list or dict |
| 527 | if (actual->tt_member != &t_unknown) |
| 528 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 529 | } |
| 530 | else if (expected->tt_type == VAR_FUNC) |
| 531 | { |
| 532 | if (expected->tt_member != &t_unknown) |
| 533 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 534 | if (ret == OK && expected->tt_argcount != -1 |
| 535 | && (actual->tt_argcount < expected->tt_min_argcount |
| 536 | || actual->tt_argcount > expected->tt_argcount)) |
| 537 | ret = FAIL; |
| 538 | } |
| 539 | if (ret == FAIL && give_msg) |
| 540 | type_mismatch(expected, actual); |
| 541 | } |
| 542 | return ret; |
| 543 | } |
| 544 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 545 | ///////////////////////////////////////////////////////////////////// |
| 546 | // Following generate_ functions expect the caller to call ga_grow(). |
| 547 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 548 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL |
| 549 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK |
| 550 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 551 | /* |
| 552 | * Generate an instruction without arguments. |
| 553 | * Returns a pointer to the new instruction, NULL if failed. |
| 554 | */ |
| 555 | static isn_T * |
| 556 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 557 | { |
| 558 | garray_T *instr = &cctx->ctx_instr; |
| 559 | isn_T *isn; |
| 560 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 561 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 562 | if (ga_grow(instr, 1) == FAIL) |
| 563 | return NULL; |
| 564 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 565 | isn->isn_type = isn_type; |
| 566 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 567 | ++instr->ga_len; |
| 568 | |
| 569 | return isn; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Generate an instruction without arguments. |
| 574 | * "drop" will be removed from the stack. |
| 575 | * Returns a pointer to the new instruction, NULL if failed. |
| 576 | */ |
| 577 | static isn_T * |
| 578 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 579 | { |
| 580 | garray_T *stack = &cctx->ctx_type_stack; |
| 581 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 582 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 583 | stack->ga_len -= drop; |
| 584 | return generate_instr(cctx, isn_type); |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 589 | */ |
| 590 | static isn_T * |
| 591 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 592 | { |
| 593 | isn_T *isn; |
| 594 | garray_T *stack = &cctx->ctx_type_stack; |
| 595 | |
| 596 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 597 | return NULL; |
| 598 | |
| 599 | if (ga_grow(stack, 1) == FAIL) |
| 600 | return NULL; |
| 601 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 602 | ++stack->ga_len; |
| 603 | |
| 604 | return isn; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 609 | */ |
| 610 | static int |
| 611 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 612 | { |
| 613 | isn_T *isn; |
| 614 | garray_T *stack = &cctx->ctx_type_stack; |
| 615 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 616 | |
| 617 | if ((*type)->tt_type == VAR_STRING) |
| 618 | return OK; |
| 619 | *type = &t_string; |
| 620 | |
| 621 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 622 | return FAIL; |
| 623 | isn->isn_arg.number = offset; |
| 624 | |
| 625 | return OK; |
| 626 | } |
| 627 | |
| 628 | static int |
| 629 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 630 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 631 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 632 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 633 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 634 | { |
| 635 | if (*op == '+') |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 636 | emsg(_("E1035: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 637 | else |
| 638 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 639 | return FAIL; |
| 640 | } |
| 641 | return OK; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Generate an instruction with two arguments. The instruction depends on the |
| 646 | * type of the arguments. |
| 647 | */ |
| 648 | static int |
| 649 | generate_two_op(cctx_T *cctx, char_u *op) |
| 650 | { |
| 651 | garray_T *stack = &cctx->ctx_type_stack; |
| 652 | type_T *type1; |
| 653 | type_T *type2; |
| 654 | vartype_T vartype; |
| 655 | isn_T *isn; |
| 656 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 657 | RETURN_OK_IF_SKIP(cctx); |
| 658 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 659 | // Get the known type of the two items on the stack. If they are matching |
| 660 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 661 | // checking. |
| 662 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 663 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 664 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 665 | if (type1->tt_type == type2->tt_type |
| 666 | && (type1->tt_type == VAR_NUMBER |
| 667 | || type1->tt_type == VAR_LIST |
| 668 | #ifdef FEAT_FLOAT |
| 669 | || type1->tt_type == VAR_FLOAT |
| 670 | #endif |
| 671 | || type1->tt_type == VAR_BLOB)) |
| 672 | vartype = type1->tt_type; |
| 673 | |
| 674 | switch (*op) |
| 675 | { |
| 676 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 677 | && type1->tt_type != VAR_ANY |
| 678 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 679 | && check_number_or_float( |
| 680 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 681 | return FAIL; |
| 682 | isn = generate_instr_drop(cctx, |
| 683 | vartype == VAR_NUMBER ? ISN_OPNR |
| 684 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 685 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 686 | #ifdef FEAT_FLOAT |
| 687 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 688 | #endif |
| 689 | : ISN_OPANY, 1); |
| 690 | if (isn != NULL) |
| 691 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 692 | break; |
| 693 | |
| 694 | case '-': |
| 695 | case '*': |
| 696 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 697 | op) == FAIL) |
| 698 | return FAIL; |
| 699 | if (vartype == VAR_NUMBER) |
| 700 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 701 | #ifdef FEAT_FLOAT |
| 702 | else if (vartype == VAR_FLOAT) |
| 703 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 704 | #endif |
| 705 | else |
| 706 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 707 | if (isn != NULL) |
| 708 | isn->isn_arg.op.op_type = *op == '*' |
| 709 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 710 | break; |
| 711 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 712 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 713 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 714 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 715 | && type2->tt_type != VAR_NUMBER)) |
| 716 | { |
| 717 | emsg(_("E1035: % requires number arguments")); |
| 718 | return FAIL; |
| 719 | } |
| 720 | isn = generate_instr_drop(cctx, |
| 721 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 722 | if (isn != NULL) |
| 723 | isn->isn_arg.op.op_type = EXPR_REM; |
| 724 | break; |
| 725 | } |
| 726 | |
| 727 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 728 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 729 | { |
| 730 | type_T *type = &t_any; |
| 731 | |
| 732 | #ifdef FEAT_FLOAT |
| 733 | // float+number and number+float results in float |
| 734 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 735 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 736 | type = &t_float; |
| 737 | #endif |
| 738 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 739 | } |
| 740 | |
| 741 | return OK; |
| 742 | } |
| 743 | |
| 744 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 745 | * Get the instruction to use for comparing "type1" with "type2" |
| 746 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 747 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 748 | static isntype_T |
| 749 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 750 | { |
| 751 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 752 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 753 | if (type1 == VAR_UNKNOWN) |
| 754 | type1 = VAR_ANY; |
| 755 | if (type2 == VAR_UNKNOWN) |
| 756 | type2 = VAR_ANY; |
| 757 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 758 | if (type1 == type2) |
| 759 | { |
| 760 | switch (type1) |
| 761 | { |
| 762 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 763 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 764 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 765 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 766 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 767 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 768 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 769 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 770 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 771 | default: isntype = ISN_COMPAREANY; break; |
| 772 | } |
| 773 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 774 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 775 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 776 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 777 | isntype = ISN_COMPAREANY; |
| 778 | |
| 779 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 780 | && (isntype == ISN_COMPAREBOOL |
| 781 | || isntype == ISN_COMPARESPECIAL |
| 782 | || isntype == ISN_COMPARENR |
| 783 | || isntype == ISN_COMPAREFLOAT)) |
| 784 | { |
| 785 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 786 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 787 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 788 | } |
| 789 | if (isntype == ISN_DROP |
| 790 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 791 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 792 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 793 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 794 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 795 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 796 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 797 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 798 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 799 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 800 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 801 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 802 | return isntype; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 807 | */ |
| 808 | static int |
| 809 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 810 | { |
| 811 | isntype_T isntype; |
| 812 | isn_T *isn; |
| 813 | garray_T *stack = &cctx->ctx_type_stack; |
| 814 | vartype_T type1; |
| 815 | vartype_T type2; |
| 816 | |
| 817 | RETURN_OK_IF_SKIP(cctx); |
| 818 | |
| 819 | // Get the known type of the two items on the stack. If they are matching |
| 820 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 821 | // checking. |
| 822 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 823 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 824 | isntype = get_compare_isn(exptype, type1, type2); |
| 825 | if (isntype == ISN_DROP) |
| 826 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 827 | |
| 828 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 829 | return FAIL; |
| 830 | isn->isn_arg.op.op_type = exptype; |
| 831 | isn->isn_arg.op.op_ic = ic; |
| 832 | |
| 833 | // takes two arguments, puts one bool back |
| 834 | if (stack->ga_len >= 2) |
| 835 | { |
| 836 | --stack->ga_len; |
| 837 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 838 | } |
| 839 | |
| 840 | return OK; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Generate an ISN_2BOOL instruction. |
| 845 | */ |
| 846 | static int |
| 847 | generate_2BOOL(cctx_T *cctx, int invert) |
| 848 | { |
| 849 | isn_T *isn; |
| 850 | garray_T *stack = &cctx->ctx_type_stack; |
| 851 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 852 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 853 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 854 | return FAIL; |
| 855 | isn->isn_arg.number = invert; |
| 856 | |
| 857 | // type becomes bool |
| 858 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 859 | |
| 860 | return OK; |
| 861 | } |
| 862 | |
| 863 | static int |
| 864 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 865 | { |
| 866 | isn_T *isn; |
| 867 | garray_T *stack = &cctx->ctx_type_stack; |
| 868 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 869 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 870 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 871 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 872 | // TODO: whole type, e.g. for a function also arg and return types |
| 873 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | isn->isn_arg.type.ct_off = offset; |
| 875 | |
| 876 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 877 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 878 | |
| 879 | return OK; |
| 880 | } |
| 881 | |
| 882 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 883 | * Check that |
| 884 | * - "actual" is "expected" type or |
| 885 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 886 | * - return FAIL. |
| 887 | */ |
| 888 | static int |
| 889 | need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx) |
| 890 | { |
| 891 | if (check_type(expected, actual, FALSE) == OK) |
| 892 | return OK; |
| 893 | if (actual->tt_type != VAR_ANY |
| 894 | && actual->tt_type != VAR_UNKNOWN |
| 895 | && !(actual->tt_type == VAR_FUNC |
| 896 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 897 | { |
| 898 | type_mismatch(expected, actual); |
| 899 | return FAIL; |
| 900 | } |
| 901 | generate_TYPECHECK(cctx, expected, offset); |
| 902 | return OK; |
| 903 | } |
| 904 | |
| 905 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 906 | * Generate an ISN_PUSHNR instruction. |
| 907 | */ |
| 908 | static int |
| 909 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 910 | { |
| 911 | isn_T *isn; |
| 912 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 913 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 914 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 915 | return FAIL; |
| 916 | isn->isn_arg.number = number; |
| 917 | |
| 918 | return OK; |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * Generate an ISN_PUSHBOOL instruction. |
| 923 | */ |
| 924 | static int |
| 925 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 926 | { |
| 927 | isn_T *isn; |
| 928 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 929 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 930 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 931 | return FAIL; |
| 932 | isn->isn_arg.number = number; |
| 933 | |
| 934 | return OK; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Generate an ISN_PUSHSPEC instruction. |
| 939 | */ |
| 940 | static int |
| 941 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 942 | { |
| 943 | isn_T *isn; |
| 944 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 945 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 946 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 947 | return FAIL; |
| 948 | isn->isn_arg.number = number; |
| 949 | |
| 950 | return OK; |
| 951 | } |
| 952 | |
| 953 | #ifdef FEAT_FLOAT |
| 954 | /* |
| 955 | * Generate an ISN_PUSHF instruction. |
| 956 | */ |
| 957 | static int |
| 958 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 959 | { |
| 960 | isn_T *isn; |
| 961 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 962 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 963 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 964 | return FAIL; |
| 965 | isn->isn_arg.fnumber = fnumber; |
| 966 | |
| 967 | return OK; |
| 968 | } |
| 969 | #endif |
| 970 | |
| 971 | /* |
| 972 | * Generate an ISN_PUSHS instruction. |
| 973 | * Consumes "str". |
| 974 | */ |
| 975 | static int |
| 976 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 977 | { |
| 978 | isn_T *isn; |
| 979 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 980 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 981 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 982 | return FAIL; |
| 983 | isn->isn_arg.string = str; |
| 984 | |
| 985 | return OK; |
| 986 | } |
| 987 | |
| 988 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 989 | * Generate an ISN_PUSHCHANNEL instruction. |
| 990 | * Consumes "channel". |
| 991 | */ |
| 992 | static int |
| 993 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 994 | { |
| 995 | isn_T *isn; |
| 996 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 997 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 998 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 999 | return FAIL; |
| 1000 | isn->isn_arg.channel = channel; |
| 1001 | |
| 1002 | return OK; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * Generate an ISN_PUSHJOB instruction. |
| 1007 | * Consumes "job". |
| 1008 | */ |
| 1009 | static int |
| 1010 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 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 | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1015 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1016 | return FAIL; |
| 1017 | isn->isn_arg.job = job; |
| 1018 | |
| 1019 | return OK; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1023 | * Generate an ISN_PUSHBLOB instruction. |
| 1024 | * Consumes "blob". |
| 1025 | */ |
| 1026 | static int |
| 1027 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1028 | { |
| 1029 | isn_T *isn; |
| 1030 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1031 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1032 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1033 | return FAIL; |
| 1034 | isn->isn_arg.blob = blob; |
| 1035 | |
| 1036 | return OK; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1040 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1041 | * Consumes "name". |
| 1042 | */ |
| 1043 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1044 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1045 | { |
| 1046 | isn_T *isn; |
| 1047 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1048 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1049 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1050 | return FAIL; |
| 1051 | isn->isn_arg.string = name; |
| 1052 | |
| 1053 | return OK; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1057 | * Generate an ISN_STORE instruction. |
| 1058 | */ |
| 1059 | static int |
| 1060 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1061 | { |
| 1062 | isn_T *isn; |
| 1063 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1064 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1065 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1066 | return FAIL; |
| 1067 | if (name != NULL) |
| 1068 | isn->isn_arg.string = vim_strsave(name); |
| 1069 | else |
| 1070 | isn->isn_arg.number = idx; |
| 1071 | |
| 1072 | return OK; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1077 | */ |
| 1078 | static int |
| 1079 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1080 | { |
| 1081 | isn_T *isn; |
| 1082 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1083 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1084 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1085 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1086 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1087 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1088 | |
| 1089 | return OK; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Generate an ISN_STOREOPT instruction |
| 1094 | */ |
| 1095 | static int |
| 1096 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1097 | { |
| 1098 | isn_T *isn; |
| 1099 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1100 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1101 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1102 | return FAIL; |
| 1103 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1104 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1105 | |
| 1106 | return OK; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Generate an ISN_LOAD or similar instruction. |
| 1111 | */ |
| 1112 | static int |
| 1113 | generate_LOAD( |
| 1114 | cctx_T *cctx, |
| 1115 | isntype_T isn_type, |
| 1116 | int idx, |
| 1117 | char_u *name, |
| 1118 | type_T *type) |
| 1119 | { |
| 1120 | isn_T *isn; |
| 1121 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1122 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1123 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1124 | return FAIL; |
| 1125 | if (name != NULL) |
| 1126 | isn->isn_arg.string = vim_strsave(name); |
| 1127 | else |
| 1128 | isn->isn_arg.number = idx; |
| 1129 | |
| 1130 | return OK; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1134 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1135 | */ |
| 1136 | static int |
| 1137 | generate_LOADV( |
| 1138 | cctx_T *cctx, |
| 1139 | char_u *name, |
| 1140 | int error) |
| 1141 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1142 | int di_flags; |
| 1143 | int vidx = find_vim_var(name, &di_flags); |
| 1144 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1145 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1146 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1147 | if (vidx < 0) |
| 1148 | { |
| 1149 | if (error) |
| 1150 | semsg(_(e_var_notfound), name); |
| 1151 | return FAIL; |
| 1152 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1153 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1154 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1155 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1159 | * Generate an ISN_UNLET instruction. |
| 1160 | */ |
| 1161 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1162 | 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] | 1163 | { |
| 1164 | isn_T *isn; |
| 1165 | |
| 1166 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1167 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1168 | return FAIL; |
| 1169 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1170 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1171 | |
| 1172 | return OK; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1176 | * Generate an ISN_LOADS instruction. |
| 1177 | */ |
| 1178 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1179 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1180 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1181 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1182 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1183 | int sid, |
| 1184 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1185 | { |
| 1186 | isn_T *isn; |
| 1187 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1188 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1189 | if (isn_type == ISN_LOADS) |
| 1190 | isn = generate_instr_type(cctx, isn_type, type); |
| 1191 | else |
| 1192 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1193 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1194 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1195 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1196 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1197 | |
| 1198 | return OK; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1203 | */ |
| 1204 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1205 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1206 | cctx_T *cctx, |
| 1207 | isntype_T isn_type, |
| 1208 | int sid, |
| 1209 | int idx, |
| 1210 | type_T *type) |
| 1211 | { |
| 1212 | isn_T *isn; |
| 1213 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1214 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1215 | if (isn_type == ISN_LOADSCRIPT) |
| 1216 | isn = generate_instr_type(cctx, isn_type, type); |
| 1217 | else |
| 1218 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1219 | if (isn == NULL) |
| 1220 | return FAIL; |
| 1221 | isn->isn_arg.script.script_sid = sid; |
| 1222 | isn->isn_arg.script.script_idx = idx; |
| 1223 | return OK; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Generate an ISN_NEWLIST instruction. |
| 1228 | */ |
| 1229 | static int |
| 1230 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1231 | { |
| 1232 | isn_T *isn; |
| 1233 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1234 | type_T *type; |
| 1235 | type_T *member; |
| 1236 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1237 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1238 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1239 | return FAIL; |
| 1240 | isn->isn_arg.number = count; |
| 1241 | |
| 1242 | // drop the value types |
| 1243 | stack->ga_len -= count; |
| 1244 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1245 | // 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] | 1246 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1247 | if (count > 0) |
| 1248 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1249 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1250 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1251 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1252 | |
| 1253 | // add the list type to the type stack |
| 1254 | if (ga_grow(stack, 1) == FAIL) |
| 1255 | return FAIL; |
| 1256 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1257 | ++stack->ga_len; |
| 1258 | |
| 1259 | return OK; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * Generate an ISN_NEWDICT instruction. |
| 1264 | */ |
| 1265 | static int |
| 1266 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1267 | { |
| 1268 | isn_T *isn; |
| 1269 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1270 | type_T *type; |
| 1271 | type_T *member; |
| 1272 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1273 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1274 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1275 | return FAIL; |
| 1276 | isn->isn_arg.number = count; |
| 1277 | |
| 1278 | // drop the key and value types |
| 1279 | stack->ga_len -= 2 * count; |
| 1280 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1281 | // Use the first value type for the list member type. Use "void" for an |
| 1282 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1283 | if (count > 0) |
| 1284 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1285 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1286 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1287 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1288 | |
| 1289 | // add the dict type to the type stack |
| 1290 | if (ga_grow(stack, 1) == FAIL) |
| 1291 | return FAIL; |
| 1292 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1293 | ++stack->ga_len; |
| 1294 | |
| 1295 | return OK; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * Generate an ISN_FUNCREF instruction. |
| 1300 | */ |
| 1301 | static int |
| 1302 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1303 | { |
| 1304 | isn_T *isn; |
| 1305 | garray_T *stack = &cctx->ctx_type_stack; |
| 1306 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1307 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1308 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1309 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1310 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1311 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1312 | |
| 1313 | if (ga_grow(stack, 1) == FAIL) |
| 1314 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1315 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1316 | // TODO: argument and return types |
| 1317 | ++stack->ga_len; |
| 1318 | |
| 1319 | return OK; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Generate an ISN_JUMP instruction. |
| 1324 | */ |
| 1325 | static int |
| 1326 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1327 | { |
| 1328 | isn_T *isn; |
| 1329 | garray_T *stack = &cctx->ctx_type_stack; |
| 1330 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1331 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1332 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1333 | return FAIL; |
| 1334 | isn->isn_arg.jump.jump_when = when; |
| 1335 | isn->isn_arg.jump.jump_where = where; |
| 1336 | |
| 1337 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1338 | --stack->ga_len; |
| 1339 | |
| 1340 | return OK; |
| 1341 | } |
| 1342 | |
| 1343 | static int |
| 1344 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1345 | { |
| 1346 | isn_T *isn; |
| 1347 | garray_T *stack = &cctx->ctx_type_stack; |
| 1348 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1349 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1350 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1351 | return FAIL; |
| 1352 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1353 | |
| 1354 | if (ga_grow(stack, 1) == FAIL) |
| 1355 | return FAIL; |
| 1356 | // type doesn't matter, will be stored next |
| 1357 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1358 | ++stack->ga_len; |
| 1359 | |
| 1360 | return OK; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Generate an ISN_BCALL instruction. |
| 1365 | * Return FAIL if the number of arguments is wrong. |
| 1366 | */ |
| 1367 | static int |
| 1368 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount) |
| 1369 | { |
| 1370 | isn_T *isn; |
| 1371 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1372 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1373 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1374 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1375 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1376 | if (check_internal_func(func_idx, argcount) == FAIL) |
| 1377 | return FAIL; |
| 1378 | |
| 1379 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1380 | return FAIL; |
| 1381 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1382 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1383 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1384 | for (i = 0; i < argcount; ++i) |
| 1385 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1386 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1387 | stack->ga_len -= argcount; // drop the arguments |
| 1388 | if (ga_grow(stack, 1) == FAIL) |
| 1389 | return FAIL; |
| 1390 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1391 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1392 | ++stack->ga_len; // add return value |
| 1393 | |
| 1394 | return OK; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1399 | * Return FAIL if the number of arguments is wrong. |
| 1400 | */ |
| 1401 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1402 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1403 | { |
| 1404 | isn_T *isn; |
| 1405 | garray_T *stack = &cctx->ctx_type_stack; |
| 1406 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1407 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1408 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1409 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1410 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1411 | { |
| 1412 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1413 | return FAIL; |
| 1414 | } |
| 1415 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1416 | { |
| 1417 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1418 | return FAIL; |
| 1419 | } |
| 1420 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1421 | if (ufunc->uf_dfunc_idx >= 0) |
| 1422 | { |
| 1423 | int i; |
| 1424 | |
| 1425 | for (i = 0; i < argcount; ++i) |
| 1426 | { |
| 1427 | type_T *expected; |
| 1428 | type_T *actual; |
| 1429 | |
| 1430 | if (i < regular_args) |
| 1431 | { |
| 1432 | if (ufunc->uf_arg_types == NULL) |
| 1433 | continue; |
| 1434 | expected = ufunc->uf_arg_types[i]; |
| 1435 | } |
| 1436 | else |
| 1437 | expected = ufunc->uf_va_type->tt_member; |
| 1438 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1439 | if (need_type(actual, expected, -argcount + i, cctx) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1440 | { |
| 1441 | arg_type_mismatch(expected, actual, i + 1); |
| 1442 | return FAIL; |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1447 | if ((isn = generate_instr(cctx, |
| 1448 | ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL) |
| 1449 | return FAIL; |
| 1450 | if (ufunc->uf_dfunc_idx >= 0) |
| 1451 | { |
| 1452 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1453 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1454 | } |
| 1455 | else |
| 1456 | { |
| 1457 | // A user function may be deleted and redefined later, can't use the |
| 1458 | // ufunc pointer, need to look it up again at runtime. |
| 1459 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1460 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1461 | } |
| 1462 | |
| 1463 | stack->ga_len -= argcount; // drop the arguments |
| 1464 | if (ga_grow(stack, 1) == FAIL) |
| 1465 | return FAIL; |
| 1466 | // add return value |
| 1467 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1468 | ++stack->ga_len; |
| 1469 | |
| 1470 | return OK; |
| 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1475 | */ |
| 1476 | static int |
| 1477 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1478 | { |
| 1479 | isn_T *isn; |
| 1480 | garray_T *stack = &cctx->ctx_type_stack; |
| 1481 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1482 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1483 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1484 | return FAIL; |
| 1485 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1486 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1487 | |
| 1488 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1489 | if (ga_grow(stack, 1) == FAIL) |
| 1490 | return FAIL; |
| 1491 | // add return value |
| 1492 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1493 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1494 | |
| 1495 | return OK; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1500 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1501 | */ |
| 1502 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1503 | generate_PCALL( |
| 1504 | cctx_T *cctx, |
| 1505 | int argcount, |
| 1506 | char_u *name, |
| 1507 | type_T *type, |
| 1508 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1509 | { |
| 1510 | isn_T *isn; |
| 1511 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1512 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1513 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1514 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1515 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1516 | if (type->tt_type == VAR_ANY) |
| 1517 | ret_type = &t_any; |
| 1518 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1519 | { |
| 1520 | if (type->tt_argcount != -1) |
| 1521 | { |
| 1522 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1523 | |
| 1524 | if (argcount < type->tt_min_argcount - varargs) |
| 1525 | { |
| 1526 | semsg(_(e_toofewarg), "[reference]"); |
| 1527 | return FAIL; |
| 1528 | } |
| 1529 | if (!varargs && argcount > type->tt_argcount) |
| 1530 | { |
| 1531 | semsg(_(e_toomanyarg), "[reference]"); |
| 1532 | return FAIL; |
| 1533 | } |
| 1534 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1535 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1536 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1537 | else |
| 1538 | { |
| 1539 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1540 | return FAIL; |
| 1541 | } |
| 1542 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1543 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1544 | return FAIL; |
| 1545 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1546 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1547 | |
| 1548 | stack->ga_len -= argcount; // drop the arguments |
| 1549 | |
| 1550 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1551 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1552 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1553 | // If partial is above the arguments it must be cleared and replaced with |
| 1554 | // the return value. |
| 1555 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1556 | return FAIL; |
| 1557 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1558 | return OK; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1562 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1563 | */ |
| 1564 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1565 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1566 | { |
| 1567 | isn_T *isn; |
| 1568 | garray_T *stack = &cctx->ctx_type_stack; |
| 1569 | type_T *type; |
| 1570 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1571 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1572 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | return FAIL; |
| 1574 | isn->isn_arg.string = vim_strnsave(name, (int)len); |
| 1575 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1576 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1577 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1578 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1579 | { |
| 1580 | emsg(_(e_dictreq)); |
| 1581 | return FAIL; |
| 1582 | } |
| 1583 | // change dict type to dict member type |
| 1584 | if (type->tt_type == VAR_DICT) |
| 1585 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1586 | |
| 1587 | return OK; |
| 1588 | } |
| 1589 | |
| 1590 | /* |
| 1591 | * Generate an ISN_ECHO instruction. |
| 1592 | */ |
| 1593 | static int |
| 1594 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1595 | { |
| 1596 | isn_T *isn; |
| 1597 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1598 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1599 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1600 | return FAIL; |
| 1601 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1602 | isn->isn_arg.echo.echo_count = count; |
| 1603 | |
| 1604 | return OK; |
| 1605 | } |
| 1606 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1607 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1608 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1609 | */ |
| 1610 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1611 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1612 | { |
| 1613 | isn_T *isn; |
| 1614 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1615 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1616 | return FAIL; |
| 1617 | isn->isn_arg.number = count; |
| 1618 | |
| 1619 | return OK; |
| 1620 | } |
| 1621 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | static int |
| 1623 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1624 | { |
| 1625 | isn_T *isn; |
| 1626 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1627 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1628 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1629 | return FAIL; |
| 1630 | isn->isn_arg.string = vim_strsave(line); |
| 1631 | return OK; |
| 1632 | } |
| 1633 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1634 | static int |
| 1635 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1636 | { |
| 1637 | isn_T *isn; |
| 1638 | |
| 1639 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1640 | return FAIL; |
| 1641 | isn->isn_arg.number = count; |
| 1642 | return OK; |
| 1643 | } |
| 1644 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1645 | /* |
| 1646 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1647 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1648 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1649 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1650 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1651 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1652 | lvar_T *lvar; |
| 1653 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1654 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1655 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1656 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1657 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1661 | return NULL; |
| 1662 | 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] | 1663 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1664 | // Every local variable uses the next entry on the stack. We could re-use |
| 1665 | // the last ones when leaving a scope, but then variables used in a closure |
| 1666 | // might get overwritten. To keep things simple do not re-use stack |
| 1667 | // entries. This is less efficient, but memory is cheap these days. |
| 1668 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1669 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1670 | lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len)); |
| 1671 | lvar->lv_const = isConst; |
| 1672 | lvar->lv_type = type; |
| 1673 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1674 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1678 | * Remove local variables above "new_top". |
| 1679 | */ |
| 1680 | static void |
| 1681 | unwind_locals(cctx_T *cctx, int new_top) |
| 1682 | { |
| 1683 | if (cctx->ctx_locals.ga_len > new_top) |
| 1684 | { |
| 1685 | int idx; |
| 1686 | lvar_T *lvar; |
| 1687 | |
| 1688 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1689 | { |
| 1690 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1691 | vim_free(lvar->lv_name); |
| 1692 | } |
| 1693 | } |
| 1694 | cctx->ctx_locals.ga_len = new_top; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Free all local variables. |
| 1699 | */ |
| 1700 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1701 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1702 | { |
| 1703 | unwind_locals(cctx, 0); |
| 1704 | ga_clear(&cctx->ctx_locals); |
| 1705 | } |
| 1706 | |
| 1707 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1708 | * Skip over a type definition and return a pointer to just after it. |
| 1709 | */ |
| 1710 | char_u * |
| 1711 | skip_type(char_u *start) |
| 1712 | { |
| 1713 | char_u *p = start; |
| 1714 | |
| 1715 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1716 | ++p; |
| 1717 | |
| 1718 | // Skip over "<type>"; this is permissive about white space. |
| 1719 | if (*skipwhite(p) == '<') |
| 1720 | { |
| 1721 | p = skipwhite(p); |
| 1722 | p = skip_type(skipwhite(p + 1)); |
| 1723 | p = skipwhite(p); |
| 1724 | if (*p == '>') |
| 1725 | ++p; |
| 1726 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1727 | else if (*p == '(' && STRNCMP("func", start, 4) == 0) |
| 1728 | { |
| 1729 | // handle func(args): type |
| 1730 | ++p; |
| 1731 | while (*p != ')' && *p != NUL) |
| 1732 | { |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1733 | char_u *sp = p; |
| 1734 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1735 | p = skip_type(p); |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1736 | if (p == sp) |
| 1737 | return p; // syntax error |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1738 | if (*p == ',') |
| 1739 | p = skipwhite(p + 1); |
| 1740 | } |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1741 | if (*p == ')') |
| 1742 | { |
| 1743 | if (p[1] == ':') |
| 1744 | p = skip_type(skipwhite(p + 2)); |
| 1745 | else |
| 1746 | p = skipwhite(p + 1); |
| 1747 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1750 | return p; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1755 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1756 | * Returns NULL in case of failure. |
| 1757 | */ |
| 1758 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1759 | 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] | 1760 | { |
| 1761 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1762 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1763 | |
| 1764 | if (**arg != '<') |
| 1765 | { |
| 1766 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1767 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1768 | else |
| 1769 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1770 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1771 | } |
| 1772 | *arg = skipwhite(*arg + 1); |
| 1773 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1774 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1775 | |
| 1776 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1777 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1778 | { |
| 1779 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1780 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1781 | } |
| 1782 | ++*arg; |
| 1783 | |
| 1784 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1785 | return get_list_type(member_type, type_gap); |
| 1786 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1791 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1792 | */ |
| 1793 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1794 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1795 | { |
| 1796 | char_u *p = *arg; |
| 1797 | size_t len; |
| 1798 | |
| 1799 | // skip over the first word |
| 1800 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1801 | ++p; |
| 1802 | len = p - *arg; |
| 1803 | |
| 1804 | switch (**arg) |
| 1805 | { |
| 1806 | case 'a': |
| 1807 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1808 | { |
| 1809 | *arg += len; |
| 1810 | return &t_any; |
| 1811 | } |
| 1812 | break; |
| 1813 | case 'b': |
| 1814 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1815 | { |
| 1816 | *arg += len; |
| 1817 | return &t_bool; |
| 1818 | } |
| 1819 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1820 | { |
| 1821 | *arg += len; |
| 1822 | return &t_blob; |
| 1823 | } |
| 1824 | break; |
| 1825 | case 'c': |
| 1826 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1827 | { |
| 1828 | *arg += len; |
| 1829 | return &t_channel; |
| 1830 | } |
| 1831 | break; |
| 1832 | case 'd': |
| 1833 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1834 | { |
| 1835 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1836 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1837 | } |
| 1838 | break; |
| 1839 | case 'f': |
| 1840 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1841 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1842 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1843 | *arg += len; |
| 1844 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1845 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1846 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1847 | return &t_any; |
| 1848 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1849 | } |
| 1850 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 1851 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1852 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1853 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1854 | int argcount = -1; |
| 1855 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1856 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1857 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 1858 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1859 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1860 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1861 | if (**arg == '(') |
| 1862 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1863 | // "func" may or may not return a value, "func()" does |
| 1864 | // not return a value. |
| 1865 | ret_type = &t_void; |
| 1866 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1867 | p = ++*arg; |
| 1868 | argcount = 0; |
| 1869 | while (*p != NUL && *p != ')') |
| 1870 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1871 | if (*p == '?') |
| 1872 | { |
| 1873 | if (first_optional == -1) |
| 1874 | first_optional = argcount; |
| 1875 | ++p; |
| 1876 | } |
| 1877 | else if (first_optional != -1) |
| 1878 | { |
| 1879 | emsg(_("E1007: mandatory argument after optional argument")); |
| 1880 | return &t_any; |
| 1881 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1882 | else if (STRNCMP(p, "...", 3) == 0) |
| 1883 | { |
| 1884 | flags |= TTFLAG_VARARGS; |
| 1885 | p += 3; |
| 1886 | } |
| 1887 | |
| 1888 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 1889 | |
| 1890 | // Nothing comes after "...{type}". |
| 1891 | if (flags & TTFLAG_VARARGS) |
| 1892 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1893 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1894 | if (*p != ',' && *skipwhite(p) == ',') |
| 1895 | { |
| 1896 | semsg(_(e_no_white_before), ","); |
| 1897 | return &t_any; |
| 1898 | } |
| 1899 | if (*p == ',') |
| 1900 | { |
| 1901 | ++p; |
| 1902 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1903 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1904 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1905 | return &t_any; |
| 1906 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1907 | } |
| 1908 | p = skipwhite(p); |
| 1909 | if (argcount == MAX_FUNC_ARGS) |
| 1910 | { |
| 1911 | emsg(_("E740: Too many argument types")); |
| 1912 | return &t_any; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | p = skipwhite(p); |
| 1917 | if (*p != ')') |
| 1918 | { |
| 1919 | emsg(_(e_missing_close)); |
| 1920 | return &t_any; |
| 1921 | } |
| 1922 | *arg = p + 1; |
| 1923 | } |
| 1924 | if (**arg == ':') |
| 1925 | { |
| 1926 | // parse return type |
| 1927 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1928 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1929 | semsg(_(e_white_after), ":"); |
| 1930 | *arg = skipwhite(*arg); |
| 1931 | ret_type = parse_type(arg, type_gap); |
| 1932 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1933 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1934 | type = get_func_type(ret_type, argcount, type_gap); |
| 1935 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1936 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1937 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 1938 | type->tt_flags = flags; |
| 1939 | if (argcount > 0) |
| 1940 | { |
| 1941 | type->tt_argcount = argcount; |
| 1942 | type->tt_min_argcount = first_optional == -1 |
| 1943 | ? argcount : first_optional; |
| 1944 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1945 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1946 | return &t_any; |
| 1947 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1948 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1949 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1950 | } |
| 1951 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1952 | } |
| 1953 | break; |
| 1954 | case 'j': |
| 1955 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 1956 | { |
| 1957 | *arg += len; |
| 1958 | return &t_job; |
| 1959 | } |
| 1960 | break; |
| 1961 | case 'l': |
| 1962 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 1963 | { |
| 1964 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1965 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1966 | } |
| 1967 | break; |
| 1968 | case 'n': |
| 1969 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 1970 | { |
| 1971 | *arg += len; |
| 1972 | return &t_number; |
| 1973 | } |
| 1974 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1975 | case 's': |
| 1976 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 1977 | { |
| 1978 | *arg += len; |
| 1979 | return &t_string; |
| 1980 | } |
| 1981 | break; |
| 1982 | case 'v': |
| 1983 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 1984 | { |
| 1985 | *arg += len; |
| 1986 | return &t_void; |
| 1987 | } |
| 1988 | break; |
| 1989 | } |
| 1990 | |
| 1991 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 1992 | return &t_any; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * Check if "type1" and "type2" are exactly the same. |
| 1997 | */ |
| 1998 | static int |
| 1999 | equal_type(type_T *type1, type_T *type2) |
| 2000 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2001 | int i; |
| 2002 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2003 | if (type1->tt_type != type2->tt_type) |
| 2004 | return FALSE; |
| 2005 | switch (type1->tt_type) |
| 2006 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2007 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2008 | case VAR_ANY: |
| 2009 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2010 | case VAR_SPECIAL: |
| 2011 | case VAR_BOOL: |
| 2012 | case VAR_NUMBER: |
| 2013 | case VAR_FLOAT: |
| 2014 | case VAR_STRING: |
| 2015 | case VAR_BLOB: |
| 2016 | case VAR_JOB: |
| 2017 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2018 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2019 | case VAR_LIST: |
| 2020 | case VAR_DICT: |
| 2021 | return equal_type(type1->tt_member, type2->tt_member); |
| 2022 | case VAR_FUNC: |
| 2023 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2024 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2025 | || type1->tt_argcount != type2->tt_argcount) |
| 2026 | return FALSE; |
| 2027 | if (type1->tt_argcount < 0 |
| 2028 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2029 | return TRUE; |
| 2030 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2031 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2032 | return FALSE; |
| 2033 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2034 | } |
| 2035 | return TRUE; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2040 | * "type2" and "dest" may be the same. |
| 2041 | */ |
| 2042 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2043 | 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] | 2044 | { |
| 2045 | if (equal_type(type1, type2)) |
| 2046 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2047 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | if (type1->tt_type == type2->tt_type) |
| 2052 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2053 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2054 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2055 | type_T *common; |
| 2056 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2057 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2058 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2059 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2060 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2061 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2062 | return; |
| 2063 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2064 | if (type1->tt_type == VAR_FUNC) |
| 2065 | { |
| 2066 | type_T *common; |
| 2067 | |
| 2068 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2069 | if (type1->tt_argcount == type2->tt_argcount |
| 2070 | && type1->tt_argcount >= 0) |
| 2071 | { |
| 2072 | int argcount = type1->tt_argcount; |
| 2073 | int i; |
| 2074 | |
| 2075 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2076 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2077 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2078 | if (func_type_add_arg_types(*dest, argcount, |
| 2079 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2080 | for (i = 0; i < argcount; ++i) |
| 2081 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2082 | &(*dest)->tt_args[i], type_gap); |
| 2083 | } |
| 2084 | } |
| 2085 | else |
| 2086 | *dest = alloc_func_type(common, -1, type_gap); |
| 2087 | return; |
| 2088 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2091 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | char * |
| 2095 | vartype_name(vartype_T type) |
| 2096 | { |
| 2097 | switch (type) |
| 2098 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2099 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2100 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2101 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2102 | case VAR_SPECIAL: return "special"; |
| 2103 | case VAR_BOOL: return "bool"; |
| 2104 | case VAR_NUMBER: return "number"; |
| 2105 | case VAR_FLOAT: return "float"; |
| 2106 | case VAR_STRING: return "string"; |
| 2107 | case VAR_BLOB: return "blob"; |
| 2108 | case VAR_JOB: return "job"; |
| 2109 | case VAR_CHANNEL: return "channel"; |
| 2110 | case VAR_LIST: return "list"; |
| 2111 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2112 | |
| 2113 | case VAR_FUNC: |
| 2114 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2115 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2116 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Return the name of a type. |
| 2121 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2122 | */ |
| 2123 | char * |
| 2124 | type_name(type_T *type, char **tofree) |
| 2125 | { |
| 2126 | char *name = vartype_name(type->tt_type); |
| 2127 | |
| 2128 | *tofree = NULL; |
| 2129 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2130 | { |
| 2131 | char *member_free; |
| 2132 | char *member_name = type_name(type->tt_member, &member_free); |
| 2133 | size_t len; |
| 2134 | |
| 2135 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2136 | *tofree = alloc(len); |
| 2137 | if (*tofree != NULL) |
| 2138 | { |
| 2139 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2140 | vim_free(member_free); |
| 2141 | return *tofree; |
| 2142 | } |
| 2143 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2144 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2145 | { |
| 2146 | garray_T ga; |
| 2147 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2148 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2149 | |
| 2150 | ga_init2(&ga, 1, 100); |
| 2151 | if (ga_grow(&ga, 20) == FAIL) |
| 2152 | return "[unknown]"; |
| 2153 | *tofree = ga.ga_data; |
| 2154 | STRCPY(ga.ga_data, "func("); |
| 2155 | ga.ga_len += 5; |
| 2156 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2157 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2158 | { |
| 2159 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2160 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2161 | int len; |
| 2162 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2163 | if (type->tt_args == NULL) |
| 2164 | arg_type = "[unknown]"; |
| 2165 | else |
| 2166 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2167 | if (i > 0) |
| 2168 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2169 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2170 | ga.ga_len += 2; |
| 2171 | } |
| 2172 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2173 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2174 | { |
| 2175 | vim_free(arg_free); |
| 2176 | return "[unknown]"; |
| 2177 | } |
| 2178 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2179 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2180 | { |
| 2181 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2182 | ga.ga_len += 3; |
| 2183 | } |
| 2184 | else if (i >= type->tt_min_argcount) |
| 2185 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2186 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2187 | ga.ga_len += len; |
| 2188 | vim_free(arg_free); |
| 2189 | } |
| 2190 | |
| 2191 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2192 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2193 | else |
| 2194 | { |
| 2195 | char *ret_free; |
| 2196 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2197 | int len; |
| 2198 | |
| 2199 | len = (int)STRLEN(ret_name) + 4; |
| 2200 | if (ga_grow(&ga, len) == FAIL) |
| 2201 | { |
| 2202 | vim_free(ret_free); |
| 2203 | return "[unknown]"; |
| 2204 | } |
| 2205 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2206 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2207 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2208 | vim_free(ret_free); |
| 2209 | } |
| 2210 | return ga.ga_data; |
| 2211 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2212 | |
| 2213 | return name; |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * Find "name" in script-local items of script "sid". |
| 2218 | * Returns the index in "sn_var_vals" if found. |
| 2219 | * If found but not in "sn_var_vals" returns -1. |
| 2220 | * If not found returns -2. |
| 2221 | */ |
| 2222 | int |
| 2223 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2224 | { |
| 2225 | hashtab_T *ht; |
| 2226 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2227 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2228 | int idx; |
| 2229 | |
| 2230 | // First look the name up in the hashtable. |
| 2231 | if (sid <= 0 || sid > script_items.ga_len) |
| 2232 | return -1; |
| 2233 | ht = &SCRIPT_VARS(sid); |
| 2234 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2235 | if (di == NULL) |
| 2236 | return -2; |
| 2237 | |
| 2238 | // Now find the svar_T index in sn_var_vals. |
| 2239 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2240 | { |
| 2241 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2242 | |
| 2243 | if (sv->sv_tv == &di->di_tv) |
| 2244 | { |
| 2245 | if (check_writable && sv->sv_const) |
| 2246 | semsg(_(e_readonlyvar), name); |
| 2247 | return idx; |
| 2248 | } |
| 2249 | } |
| 2250 | return -1; |
| 2251 | } |
| 2252 | |
| 2253 | /* |
| 2254 | * Find "name" in imported items of the current script/ |
| 2255 | */ |
| 2256 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2257 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2258 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2259 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2260 | int idx; |
| 2261 | |
| 2262 | if (cctx != NULL) |
| 2263 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2264 | { |
| 2265 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2266 | + idx; |
| 2267 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2268 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2269 | : STRLEN(import->imp_name) == len |
| 2270 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2271 | return import; |
| 2272 | } |
| 2273 | |
| 2274 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2275 | { |
| 2276 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2277 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2278 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2279 | : STRLEN(import->imp_name) == len |
| 2280 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2281 | return import; |
| 2282 | } |
| 2283 | return NULL; |
| 2284 | } |
| 2285 | |
| 2286 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2287 | * Free all imported variables. |
| 2288 | */ |
| 2289 | static void |
| 2290 | free_imported(cctx_T *cctx) |
| 2291 | { |
| 2292 | int idx; |
| 2293 | |
| 2294 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2295 | { |
| 2296 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2297 | |
| 2298 | vim_free(import->imp_name); |
| 2299 | } |
| 2300 | ga_clear(&cctx->ctx_imports); |
| 2301 | } |
| 2302 | |
| 2303 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2304 | * Get the next line of the function from "cctx". |
| 2305 | * Returns NULL when at the end. |
| 2306 | */ |
| 2307 | static char_u * |
| 2308 | next_line_from_context(cctx_T *cctx) |
| 2309 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2310 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2311 | |
| 2312 | do |
| 2313 | { |
| 2314 | ++cctx->ctx_lnum; |
| 2315 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2316 | { |
| 2317 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2318 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2319 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2320 | 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] | 2321 | cctx->ctx_line_start = line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2322 | SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum |
| 2323 | + cctx->ctx_lnum + 1; |
Bram Moolenaar | 675f716 | 2020-04-12 22:53:54 +0200 | [diff] [blame] | 2324 | } while (line == NULL || *skipwhite(line) == NUL); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2325 | return line; |
| 2326 | } |
| 2327 | |
| 2328 | /* |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2329 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2330 | */ |
| 2331 | static int |
| 2332 | comment_start(char_u *p) |
| 2333 | { |
| 2334 | return p[0] == '#' && p[1] != '{'; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2338 | * 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] | 2339 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2340 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2341 | */ |
| 2342 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2343 | 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] | 2344 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2345 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2346 | { |
| 2347 | char_u *next = next_line_from_context(cctx); |
| 2348 | |
| 2349 | if (next == NULL) |
| 2350 | return FAIL; |
| 2351 | *arg = skipwhite(next); |
| 2352 | } |
| 2353 | return OK; |
| 2354 | } |
| 2355 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2356 | // Structure passed between the compile_expr* functions to keep track of |
| 2357 | // constants that have been parsed but for which no code was produced yet. If |
| 2358 | // possible expressions on these constants are applied at compile time. If |
| 2359 | // that is not possible, the code to push the constants needs to be generated |
| 2360 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2361 | // Using 50 should be more than enough of 5 levels of (). |
| 2362 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2363 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2364 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2365 | int pp_used; // active entries in pp_tv[] |
| 2366 | } ppconst_T; |
| 2367 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2368 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2369 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2370 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2371 | /* |
| 2372 | * Generate a PUSH instruction for "tv". |
| 2373 | * "tv" will be consumed or cleared. |
| 2374 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2375 | */ |
| 2376 | static int |
| 2377 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2378 | { |
| 2379 | if (tv != NULL) |
| 2380 | { |
| 2381 | switch (tv->v_type) |
| 2382 | { |
| 2383 | case VAR_UNKNOWN: |
| 2384 | break; |
| 2385 | case VAR_BOOL: |
| 2386 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2387 | break; |
| 2388 | case VAR_SPECIAL: |
| 2389 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2390 | break; |
| 2391 | case VAR_NUMBER: |
| 2392 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2393 | break; |
| 2394 | #ifdef FEAT_FLOAT |
| 2395 | case VAR_FLOAT: |
| 2396 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2397 | break; |
| 2398 | #endif |
| 2399 | case VAR_BLOB: |
| 2400 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2401 | tv->vval.v_blob = NULL; |
| 2402 | break; |
| 2403 | case VAR_STRING: |
| 2404 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2405 | tv->vval.v_string = NULL; |
| 2406 | break; |
| 2407 | default: |
| 2408 | iemsg("constant type not supported"); |
| 2409 | clear_tv(tv); |
| 2410 | return FAIL; |
| 2411 | } |
| 2412 | tv->v_type = VAR_UNKNOWN; |
| 2413 | } |
| 2414 | return OK; |
| 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * Generate code for any ppconst entries. |
| 2419 | */ |
| 2420 | static int |
| 2421 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2422 | { |
| 2423 | int i; |
| 2424 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2425 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2426 | |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2427 | cctx->ctx_skip = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2428 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2429 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2430 | ret = FAIL; |
| 2431 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2432 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2433 | return ret; |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Clear ppconst constants. Used when failing. |
| 2438 | */ |
| 2439 | static void |
| 2440 | clear_ppconst(ppconst_T *ppconst) |
| 2441 | { |
| 2442 | int i; |
| 2443 | |
| 2444 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2445 | clear_tv(&ppconst->pp_tv[i]); |
| 2446 | ppconst->pp_used = 0; |
| 2447 | } |
| 2448 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2449 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2450 | * Generate an instruction to load script-local variable "name", without the |
| 2451 | * leading "s:". |
| 2452 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2453 | */ |
| 2454 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2455 | compile_load_scriptvar( |
| 2456 | cctx_T *cctx, |
| 2457 | char_u *name, // variable NUL terminated |
| 2458 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2459 | char_u **end, // end of variable |
| 2460 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2461 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2462 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2463 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2464 | imported_T *import; |
| 2465 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2466 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2467 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2468 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2469 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2470 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2471 | } |
| 2472 | if (idx >= 0) |
| 2473 | { |
| 2474 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2475 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2476 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2477 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2478 | return OK; |
| 2479 | } |
| 2480 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2481 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2482 | if (import != NULL) |
| 2483 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2484 | if (import->imp_all) |
| 2485 | { |
| 2486 | char_u *p = skipwhite(*end); |
| 2487 | int name_len; |
| 2488 | ufunc_T *ufunc; |
| 2489 | type_T *type; |
| 2490 | |
| 2491 | // Used "import * as Name", need to lookup the member. |
| 2492 | if (*p != '.') |
| 2493 | { |
| 2494 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2495 | return FAIL; |
| 2496 | } |
| 2497 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2498 | if (VIM_ISWHITE(*p)) |
| 2499 | { |
| 2500 | emsg(_("E1074: no white space allowed after dot")); |
| 2501 | return FAIL; |
| 2502 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2503 | |
| 2504 | idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type); |
| 2505 | // TODO: what if it is a function? |
| 2506 | if (idx < 0) |
| 2507 | return FAIL; |
| 2508 | *end = p; |
| 2509 | |
| 2510 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2511 | import->imp_sid, |
| 2512 | idx, |
| 2513 | type); |
| 2514 | } |
| 2515 | else |
| 2516 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2517 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2518 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2519 | import->imp_sid, |
| 2520 | import->imp_var_vals_idx, |
| 2521 | import->imp_type); |
| 2522 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2523 | return OK; |
| 2524 | } |
| 2525 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2526 | if (error) |
| 2527 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2528 | return FAIL; |
| 2529 | } |
| 2530 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2531 | static int |
| 2532 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2533 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2534 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2535 | |
| 2536 | if (ufunc == NULL) |
| 2537 | return FAIL; |
| 2538 | |
| 2539 | return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type); |
| 2540 | } |
| 2541 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2542 | /* |
| 2543 | * Compile a variable name into a load instruction. |
| 2544 | * "end" points to just after the name. |
| 2545 | * When "error" is FALSE do not give an error when not found. |
| 2546 | */ |
| 2547 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2548 | 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] | 2549 | { |
| 2550 | type_T *type; |
| 2551 | char_u *name; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2552 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2553 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2554 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2555 | |
| 2556 | if (*(*arg + 1) == ':') |
| 2557 | { |
| 2558 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2559 | if (end <= *arg + 2) |
| 2560 | name = vim_strsave((char_u *)"[empty]"); |
| 2561 | else |
| 2562 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2563 | if (name == NULL) |
| 2564 | return FAIL; |
| 2565 | |
| 2566 | if (**arg == 'v') |
| 2567 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2568 | res = generate_LOADV(cctx, name, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2569 | } |
| 2570 | else if (**arg == 'g') |
| 2571 | { |
| 2572 | // Global variables can be defined later, thus we don't check if it |
| 2573 | // exists, give error at runtime. |
| 2574 | res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); |
| 2575 | } |
| 2576 | else if (**arg == 's') |
| 2577 | { |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2578 | res = compile_load_scriptvar(cctx, name, NULL, NULL, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2579 | } |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2580 | else if (**arg == 'b') |
| 2581 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2582 | // Buffer-local variables can be defined later, thus we don't check |
| 2583 | // if it exists, give error at runtime. |
| 2584 | res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2585 | } |
| 2586 | else if (**arg == 'w') |
| 2587 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2588 | // Window-local variables can be defined later, thus we don't check |
| 2589 | // if it exists, give error at runtime. |
| 2590 | res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2591 | } |
| 2592 | else if (**arg == 't') |
| 2593 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2594 | // Tabpage-local variables can be defined later, thus we don't |
| 2595 | // check if it exists, give error at runtime. |
| 2596 | res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2597 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2598 | else |
| 2599 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2600 | semsg("E1075: Namespace not supported: %s", *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2601 | goto theend; |
| 2602 | } |
| 2603 | } |
| 2604 | else |
| 2605 | { |
| 2606 | size_t len = end - *arg; |
| 2607 | int idx; |
| 2608 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2609 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2610 | |
| 2611 | name = vim_strnsave(*arg, end - *arg); |
| 2612 | if (name == NULL) |
| 2613 | return FAIL; |
| 2614 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2615 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2616 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2617 | if (!gen_load_outer) |
| 2618 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2619 | } |
| 2620 | else |
| 2621 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2622 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2623 | |
| 2624 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2625 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2626 | type = lvar->lv_type; |
| 2627 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2628 | if (lvar->lv_from_outer) |
| 2629 | gen_load_outer = TRUE; |
| 2630 | else |
| 2631 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2632 | } |
| 2633 | else |
| 2634 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2635 | // "var" can be script-local even without using "s:" if it |
| 2636 | // already exists. |
| 2637 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2638 | == SCRIPT_VERSION_VIM9 |
| 2639 | || lookup_script(*arg, len) == OK) |
| 2640 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2641 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2642 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2643 | // When the name starts with an uppercase letter or "x:" it |
| 2644 | // can be a user defined function. |
| 2645 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2646 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | if (gen_load) |
| 2650 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2651 | if (gen_load_outer) |
| 2652 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | *arg = end; |
| 2656 | |
| 2657 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2658 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2659 | semsg(_(e_var_notfound), name); |
| 2660 | vim_free(name); |
| 2661 | return res; |
| 2662 | } |
| 2663 | |
| 2664 | /* |
| 2665 | * Compile the argument expressions. |
| 2666 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2667 | */ |
| 2668 | static int |
| 2669 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2670 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2671 | char_u *p = *arg; |
| 2672 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2673 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2674 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2675 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2676 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2677 | { |
| 2678 | p = next_line_from_context(cctx); |
| 2679 | if (p == NULL) |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2680 | goto failret; |
| 2681 | whitep = (char_u *)" "; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2682 | p = skipwhite(p); |
| 2683 | } |
| 2684 | if (*p == ')') |
| 2685 | { |
| 2686 | *arg = p + 1; |
| 2687 | return OK; |
| 2688 | } |
| 2689 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2690 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2691 | return FAIL; |
| 2692 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2693 | |
| 2694 | if (*p != ',' && *skipwhite(p) == ',') |
| 2695 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2696 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2697 | p = skipwhite(p); |
| 2698 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2699 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2700 | { |
| 2701 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2702 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2703 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2704 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2705 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2706 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2707 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2708 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2709 | emsg(_(e_missing_close)); |
| 2710 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * Compile a function call: name(arg1, arg2) |
| 2715 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2716 | * "argcount_init" is 1 for "value->method()" |
| 2717 | * Instructions: |
| 2718 | * EVAL arg1 |
| 2719 | * EVAL arg2 |
| 2720 | * BCALL / DCALL / UCALL |
| 2721 | */ |
| 2722 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2723 | compile_call( |
| 2724 | char_u **arg, |
| 2725 | size_t varlen, |
| 2726 | cctx_T *cctx, |
| 2727 | ppconst_T *ppconst, |
| 2728 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2729 | { |
| 2730 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2731 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2732 | int argcount = argcount_init; |
| 2733 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2734 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2735 | char_u *tofree = NULL; |
| 2736 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2737 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2738 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2739 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2740 | // we can evaluate "has('name')" at compile time |
| 2741 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2742 | { |
| 2743 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2744 | typval_T argvars[2]; |
| 2745 | |
| 2746 | argvars[0].v_type = VAR_UNKNOWN; |
| 2747 | if (*s == '"') |
| 2748 | (void)get_string_tv(&s, &argvars[0], TRUE); |
| 2749 | else if (*s == '\'') |
| 2750 | (void)get_lit_string_tv(&s, &argvars[0], TRUE); |
| 2751 | s = skipwhite(s); |
| 2752 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2753 | { |
| 2754 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2755 | |
| 2756 | *arg = s + 1; |
| 2757 | argvars[1].v_type = VAR_UNKNOWN; |
| 2758 | tv->v_type = VAR_NUMBER; |
| 2759 | tv->vval.v_number = 0; |
| 2760 | f_has(argvars, tv); |
| 2761 | clear_tv(&argvars[0]); |
| 2762 | ++ppconst->pp_used; |
| 2763 | return OK; |
| 2764 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2765 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2769 | return FAIL; |
| 2770 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2771 | if (varlen >= sizeof(namebuf)) |
| 2772 | { |
| 2773 | semsg(_("E1011: name too long: %s"), name); |
| 2774 | return FAIL; |
| 2775 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2776 | vim_strncpy(namebuf, *arg, varlen); |
| 2777 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2778 | |
| 2779 | *arg = skipwhite(*arg + varlen + 1); |
| 2780 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2781 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2782 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2783 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2784 | { |
| 2785 | int idx; |
| 2786 | |
| 2787 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2788 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2789 | if (idx >= 0) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2790 | res = generate_BCALL(cctx, idx, argcount); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2791 | else |
| 2792 | semsg(_(e_unknownfunc), namebuf); |
| 2793 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2794 | } |
| 2795 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2796 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2797 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2798 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2799 | { |
| 2800 | res = generate_CALL(cctx, ufunc, argcount); |
| 2801 | goto theend; |
| 2802 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2803 | |
| 2804 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2805 | // 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] | 2806 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2807 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 2808 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2809 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2810 | garray_T *stack = &cctx->ctx_type_stack; |
| 2811 | type_T *type; |
| 2812 | |
| 2813 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2814 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2815 | goto theend; |
| 2816 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2817 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2818 | // 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] | 2819 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2820 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 2821 | res = generate_UCALL(cctx, name, argcount); |
| 2822 | else |
| 2823 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2824 | |
| 2825 | theend: |
| 2826 | vim_free(tofree); |
| 2827 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2831 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2832 | |
| 2833 | /* |
| 2834 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2835 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2836 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2837 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2838 | * valid name. |
| 2839 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2840 | static char_u * |
| 2841 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2842 | { |
| 2843 | char_u *p; |
| 2844 | |
| 2845 | // Quick check for valid starting character. |
| 2846 | if (!eval_isnamec1(*arg)) |
| 2847 | return arg; |
| 2848 | |
| 2849 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2850 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2851 | // and can be used in slice "[n:]". |
| 2852 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2853 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2854 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2855 | break; |
| 2856 | return p; |
| 2857 | } |
| 2858 | |
| 2859 | /* |
| 2860 | * Like to_name_end() but also skip over a list or dict constant. |
| 2861 | */ |
| 2862 | char_u * |
| 2863 | to_name_const_end(char_u *arg) |
| 2864 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2865 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2866 | typval_T rettv; |
| 2867 | |
| 2868 | if (p == arg && *arg == '[') |
| 2869 | { |
| 2870 | |
| 2871 | // Can be "[1, 2, 3]->Func()". |
| 2872 | if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL) |
| 2873 | p = arg; |
| 2874 | } |
| 2875 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2876 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2877 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2878 | ++p; |
| 2879 | if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL) |
| 2880 | p = arg; |
| 2881 | } |
| 2882 | else if (p == arg && *arg == '{') |
| 2883 | { |
| 2884 | int ret = get_lambda_tv(&p, &rettv, FALSE); |
| 2885 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2886 | // Can be "{x -> ret}()". |
| 2887 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2888 | if (ret == NOTDONE) |
| 2889 | ret = eval_dict(&p, &rettv, FALSE, FALSE); |
| 2890 | if (ret != OK) |
| 2891 | p = arg; |
| 2892 | } |
| 2893 | |
| 2894 | return p; |
| 2895 | } |
| 2896 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2897 | /* |
| 2898 | * parse a list: [expr, expr] |
| 2899 | * "*arg" points to the '['. |
| 2900 | */ |
| 2901 | static int |
| 2902 | compile_list(char_u **arg, cctx_T *cctx) |
| 2903 | { |
| 2904 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2905 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2906 | int count = 0; |
| 2907 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2908 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2909 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2910 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2911 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2912 | p = next_line_from_context(cctx); |
| 2913 | if (p == NULL) |
| 2914 | { |
| 2915 | semsg(_(e_list_end), *arg); |
| 2916 | return FAIL; |
| 2917 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2918 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2919 | p = skipwhite(p); |
| 2920 | } |
| 2921 | if (*p == ']') |
| 2922 | { |
| 2923 | ++p; |
| 2924 | // Allow for following comment, after at least one space. |
| 2925 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '"') |
| 2926 | p += STRLEN(p); |
| 2927 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2928 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2929 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2930 | break; |
| 2931 | ++count; |
| 2932 | if (*p == ',') |
| 2933 | ++p; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2934 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2935 | p = skipwhite(p); |
| 2936 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2937 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | |
| 2939 | generate_NEWLIST(cctx, count); |
| 2940 | return OK; |
| 2941 | } |
| 2942 | |
| 2943 | /* |
| 2944 | * parse a lambda: {arg, arg -> expr} |
| 2945 | * "*arg" points to the '{'. |
| 2946 | */ |
| 2947 | static int |
| 2948 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2949 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2950 | typval_T rettv; |
| 2951 | ufunc_T *ufunc; |
| 2952 | |
| 2953 | // Get the funcref in "rettv". |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2954 | if (get_lambda_tv(arg, &rettv, TRUE) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2955 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2956 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2957 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2958 | ++ufunc->uf_refcount; |
| 2959 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2960 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | |
| 2962 | // The function will have one line: "return {expr}". |
| 2963 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2964 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2965 | |
| 2966 | if (ufunc->uf_dfunc_idx >= 0) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 2967 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2968 | return FAIL; |
| 2969 | } |
| 2970 | |
| 2971 | /* |
| 2972 | * Compile a lamda call: expr->{lambda}(args) |
| 2973 | * "arg" points to the "{". |
| 2974 | */ |
| 2975 | static int |
| 2976 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2977 | { |
| 2978 | ufunc_T *ufunc; |
| 2979 | typval_T rettv; |
| 2980 | int argcount = 1; |
| 2981 | int ret = FAIL; |
| 2982 | |
| 2983 | // Get the funcref in "rettv". |
| 2984 | if (get_lambda_tv(arg, &rettv, TRUE) == FAIL) |
| 2985 | return FAIL; |
| 2986 | |
| 2987 | if (**arg != '(') |
| 2988 | { |
| 2989 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2990 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2991 | else |
| 2992 | semsg(_(e_missing_paren), "lambda"); |
| 2993 | clear_tv(&rettv); |
| 2994 | return FAIL; |
| 2995 | } |
| 2996 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2997 | ufunc = rettv.vval.v_partial->pt_func; |
| 2998 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2999 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3000 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3001 | |
| 3002 | // The function will have one line: "return {expr}". |
| 3003 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3004 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3005 | |
| 3006 | // compile the arguments |
| 3007 | *arg = skipwhite(*arg + 1); |
| 3008 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3009 | // call the compiled function |
| 3010 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3011 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3012 | return ret; |
| 3013 | } |
| 3014 | |
| 3015 | /* |
| 3016 | * parse a dict: {'key': val} or #{key: val} |
| 3017 | * "*arg" points to the '{'. |
| 3018 | */ |
| 3019 | static int |
| 3020 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3021 | { |
| 3022 | garray_T *instr = &cctx->ctx_instr; |
| 3023 | int count = 0; |
| 3024 | dict_T *d = dict_alloc(); |
| 3025 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3026 | char_u *whitep = *arg; |
| 3027 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3028 | |
| 3029 | if (d == NULL) |
| 3030 | return FAIL; |
| 3031 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3032 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3033 | { |
| 3034 | char_u *key = NULL; |
| 3035 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3036 | while (**arg == NUL || (literal && **arg == '"') |
| 3037 | || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3038 | { |
| 3039 | *arg = next_line_from_context(cctx); |
| 3040 | if (*arg == NULL) |
| 3041 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3042 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3043 | *arg = skipwhite(*arg); |
| 3044 | } |
| 3045 | |
| 3046 | if (**arg == '}') |
| 3047 | break; |
| 3048 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3049 | if (literal) |
| 3050 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3051 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3052 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3053 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3054 | { |
| 3055 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3056 | return FAIL; |
| 3057 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3058 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3059 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3060 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3061 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3062 | } |
| 3063 | else |
| 3064 | { |
| 3065 | isn_T *isn; |
| 3066 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3067 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3068 | return FAIL; |
| 3069 | // TODO: check type is string |
| 3070 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3071 | if (isn->isn_type == ISN_PUSHS) |
| 3072 | key = isn->isn_arg.string; |
| 3073 | } |
| 3074 | |
| 3075 | // Check for duplicate keys, if using string keys. |
| 3076 | if (key != NULL) |
| 3077 | { |
| 3078 | item = dict_find(d, key, -1); |
| 3079 | if (item != NULL) |
| 3080 | { |
| 3081 | semsg(_(e_duplicate_key), key); |
| 3082 | goto failret; |
| 3083 | } |
| 3084 | item = dictitem_alloc(key); |
| 3085 | if (item != NULL) |
| 3086 | { |
| 3087 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3088 | item->di_tv.v_lock = 0; |
| 3089 | if (dict_add(d, item) == FAIL) |
| 3090 | dictitem_free(item); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | *arg = skipwhite(*arg); |
| 3095 | if (**arg != ':') |
| 3096 | { |
| 3097 | semsg(_(e_missing_dict_colon), *arg); |
| 3098 | return FAIL; |
| 3099 | } |
| 3100 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3101 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3102 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3103 | while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3104 | { |
| 3105 | *arg = next_line_from_context(cctx); |
| 3106 | if (*arg == NULL) |
| 3107 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3108 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3109 | *arg = skipwhite(*arg); |
| 3110 | } |
| 3111 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3112 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | return FAIL; |
| 3114 | ++count; |
| 3115 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3116 | whitep = *arg; |
| 3117 | p = skipwhite(*arg); |
| 3118 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3119 | { |
| 3120 | *arg = next_line_from_context(cctx); |
| 3121 | if (*arg == NULL) |
| 3122 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3123 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3124 | *arg = skipwhite(*arg); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3125 | p = *arg; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3126 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3127 | if (**arg == '}') |
| 3128 | break; |
| 3129 | if (**arg != ',') |
| 3130 | { |
| 3131 | semsg(_(e_missing_dict_comma), *arg); |
| 3132 | goto failret; |
| 3133 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3134 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3135 | *arg = skipwhite(*arg + 1); |
| 3136 | } |
| 3137 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3138 | *arg = *arg + 1; |
| 3139 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3140 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3141 | p = skipwhite(*arg); |
| 3142 | if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3143 | *arg += STRLEN(*arg); |
| 3144 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3145 | dict_unref(d); |
| 3146 | return generate_NEWDICT(cctx, count); |
| 3147 | |
| 3148 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3149 | if (*arg == NULL) |
| 3150 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3151 | dict_unref(d); |
| 3152 | return FAIL; |
| 3153 | } |
| 3154 | |
| 3155 | /* |
| 3156 | * Compile "&option". |
| 3157 | */ |
| 3158 | static int |
| 3159 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3160 | { |
| 3161 | typval_T rettv; |
| 3162 | char_u *start = *arg; |
| 3163 | int ret; |
| 3164 | |
| 3165 | // parse the option and get the current value to get the type. |
| 3166 | rettv.v_type = VAR_UNKNOWN; |
| 3167 | ret = get_option_tv(arg, &rettv, TRUE); |
| 3168 | if (ret == OK) |
| 3169 | { |
| 3170 | // include the '&' in the name, get_option_tv() expects it. |
| 3171 | char_u *name = vim_strnsave(start, *arg - start); |
| 3172 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3173 | |
| 3174 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3175 | vim_free(name); |
| 3176 | } |
| 3177 | clear_tv(&rettv); |
| 3178 | |
| 3179 | return ret; |
| 3180 | } |
| 3181 | |
| 3182 | /* |
| 3183 | * Compile "$VAR". |
| 3184 | */ |
| 3185 | static int |
| 3186 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3187 | { |
| 3188 | char_u *start = *arg; |
| 3189 | int len; |
| 3190 | int ret; |
| 3191 | char_u *name; |
| 3192 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3193 | ++*arg; |
| 3194 | len = get_env_len(arg); |
| 3195 | if (len == 0) |
| 3196 | { |
| 3197 | semsg(_(e_syntax_at), start - 1); |
| 3198 | return FAIL; |
| 3199 | } |
| 3200 | |
| 3201 | // include the '$' in the name, get_env_tv() expects it. |
| 3202 | name = vim_strnsave(start, len + 1); |
| 3203 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3204 | vim_free(name); |
| 3205 | return ret; |
| 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * Compile "@r". |
| 3210 | */ |
| 3211 | static int |
| 3212 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3213 | { |
| 3214 | int ret; |
| 3215 | |
| 3216 | ++*arg; |
| 3217 | if (**arg == NUL) |
| 3218 | { |
| 3219 | semsg(_(e_syntax_at), *arg - 1); |
| 3220 | return FAIL; |
| 3221 | } |
| 3222 | if (!valid_yank_reg(**arg, TRUE)) |
| 3223 | { |
| 3224 | emsg_invreg(**arg); |
| 3225 | return FAIL; |
| 3226 | } |
| 3227 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3228 | ++*arg; |
| 3229 | return ret; |
| 3230 | } |
| 3231 | |
| 3232 | /* |
| 3233 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3234 | */ |
| 3235 | static int |
| 3236 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3237 | { |
| 3238 | char_u *p = end; |
| 3239 | |
| 3240 | // this works from end to start |
| 3241 | while (p > start) |
| 3242 | { |
| 3243 | --p; |
| 3244 | if (*p == '-' || *p == '+') |
| 3245 | { |
| 3246 | // only '-' has an effect, for '+' we only check the type |
| 3247 | #ifdef FEAT_FLOAT |
| 3248 | if (rettv->v_type == VAR_FLOAT) |
| 3249 | { |
| 3250 | if (*p == '-') |
| 3251 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3252 | } |
| 3253 | else |
| 3254 | #endif |
| 3255 | { |
| 3256 | varnumber_T val; |
| 3257 | int error = FALSE; |
| 3258 | |
| 3259 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3260 | // here |
| 3261 | if (check_not_string(rettv) == FAIL) |
| 3262 | return FAIL; |
| 3263 | val = tv_get_number_chk(rettv, &error); |
| 3264 | clear_tv(rettv); |
| 3265 | if (error) |
| 3266 | return FAIL; |
| 3267 | if (*p == '-') |
| 3268 | val = -val; |
| 3269 | rettv->v_type = VAR_NUMBER; |
| 3270 | rettv->vval.v_number = val; |
| 3271 | } |
| 3272 | } |
| 3273 | else |
| 3274 | { |
| 3275 | int v = tv2bool(rettv); |
| 3276 | |
| 3277 | // '!' is permissive in the type. |
| 3278 | clear_tv(rettv); |
| 3279 | rettv->v_type = VAR_BOOL; |
| 3280 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3281 | } |
| 3282 | } |
| 3283 | return OK; |
| 3284 | } |
| 3285 | |
| 3286 | /* |
| 3287 | * Recognize v: variables that are constants and set "rettv". |
| 3288 | */ |
| 3289 | static void |
| 3290 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3291 | { |
| 3292 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3293 | { |
| 3294 | rettv->v_type = VAR_BOOL; |
| 3295 | rettv->vval.v_number = VVAL_TRUE; |
| 3296 | *arg += 6; |
| 3297 | } |
| 3298 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3299 | { |
| 3300 | rettv->v_type = VAR_BOOL; |
| 3301 | rettv->vval.v_number = VVAL_FALSE; |
| 3302 | *arg += 7; |
| 3303 | } |
| 3304 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3305 | { |
| 3306 | rettv->v_type = VAR_SPECIAL; |
| 3307 | rettv->vval.v_number = VVAL_NULL; |
| 3308 | *arg += 6; |
| 3309 | } |
| 3310 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3311 | { |
| 3312 | rettv->v_type = VAR_SPECIAL; |
| 3313 | rettv->vval.v_number = VVAL_NONE; |
| 3314 | *arg += 6; |
| 3315 | } |
| 3316 | } |
| 3317 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3318 | static exptype_T |
| 3319 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3320 | { |
| 3321 | exptype_T type = EXPR_UNKNOWN; |
| 3322 | int i; |
| 3323 | |
| 3324 | switch (p[0]) |
| 3325 | { |
| 3326 | case '=': if (p[1] == '=') |
| 3327 | type = EXPR_EQUAL; |
| 3328 | else if (p[1] == '~') |
| 3329 | type = EXPR_MATCH; |
| 3330 | break; |
| 3331 | case '!': if (p[1] == '=') |
| 3332 | type = EXPR_NEQUAL; |
| 3333 | else if (p[1] == '~') |
| 3334 | type = EXPR_NOMATCH; |
| 3335 | break; |
| 3336 | case '>': if (p[1] != '=') |
| 3337 | { |
| 3338 | type = EXPR_GREATER; |
| 3339 | *len = 1; |
| 3340 | } |
| 3341 | else |
| 3342 | type = EXPR_GEQUAL; |
| 3343 | break; |
| 3344 | case '<': if (p[1] != '=') |
| 3345 | { |
| 3346 | type = EXPR_SMALLER; |
| 3347 | *len = 1; |
| 3348 | } |
| 3349 | else |
| 3350 | type = EXPR_SEQUAL; |
| 3351 | break; |
| 3352 | case 'i': if (p[1] == 's') |
| 3353 | { |
| 3354 | // "is" and "isnot"; but not a prefix of a name |
| 3355 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3356 | *len = 5; |
| 3357 | i = p[*len]; |
| 3358 | if (!isalnum(i) && i != '_') |
| 3359 | { |
| 3360 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3361 | *type_is = TRUE; |
| 3362 | } |
| 3363 | } |
| 3364 | break; |
| 3365 | } |
| 3366 | return type; |
| 3367 | } |
| 3368 | |
| 3369 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3370 | * Compile code to apply '-', '+' and '!'. |
| 3371 | */ |
| 3372 | static int |
| 3373 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3374 | { |
| 3375 | char_u *p = end; |
| 3376 | |
| 3377 | // this works from end to start |
| 3378 | while (p > start) |
| 3379 | { |
| 3380 | --p; |
| 3381 | if (*p == '-' || *p == '+') |
| 3382 | { |
| 3383 | int negate = *p == '-'; |
| 3384 | isn_T *isn; |
| 3385 | |
| 3386 | // TODO: check type |
| 3387 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3388 | { |
| 3389 | --p; |
| 3390 | if (*p == '-') |
| 3391 | negate = !negate; |
| 3392 | } |
| 3393 | // only '-' has an effect, for '+' we only check the type |
| 3394 | if (negate) |
| 3395 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3396 | else |
| 3397 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3398 | if (isn == NULL) |
| 3399 | return FAIL; |
| 3400 | } |
| 3401 | else |
| 3402 | { |
| 3403 | int invert = TRUE; |
| 3404 | |
| 3405 | while (p > start && p[-1] == '!') |
| 3406 | { |
| 3407 | --p; |
| 3408 | invert = !invert; |
| 3409 | } |
| 3410 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3411 | return FAIL; |
| 3412 | } |
| 3413 | } |
| 3414 | return OK; |
| 3415 | } |
| 3416 | |
| 3417 | /* |
| 3418 | * Compile whatever comes after "name" or "name()". |
| 3419 | */ |
| 3420 | static int |
| 3421 | compile_subscript( |
| 3422 | char_u **arg, |
| 3423 | cctx_T *cctx, |
| 3424 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3425 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3426 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3427 | { |
| 3428 | for (;;) |
| 3429 | { |
| 3430 | if (**arg == '(') |
| 3431 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3432 | garray_T *stack = &cctx->ctx_type_stack; |
| 3433 | type_T *type; |
| 3434 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3435 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3436 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3437 | return FAIL; |
| 3438 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3439 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3440 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3441 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3442 | *arg = skipwhite(*arg + 1); |
| 3443 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3444 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3445 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3446 | return FAIL; |
| 3447 | } |
| 3448 | else if (**arg == '-' && (*arg)[1] == '>') |
| 3449 | { |
| 3450 | char_u *p; |
| 3451 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3452 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3453 | return FAIL; |
| 3454 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3455 | // something->method() |
| 3456 | // Apply the '!', '-' and '+' first: |
| 3457 | // -1.0->func() works like (-1.0)->func() |
| 3458 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3459 | return FAIL; |
| 3460 | *start_leader = end_leader; // don't apply again later |
| 3461 | |
| 3462 | *arg = skipwhite(*arg + 2); |
| 3463 | if (**arg == '{') |
| 3464 | { |
| 3465 | // lambda call: list->{lambda} |
| 3466 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3467 | return FAIL; |
| 3468 | } |
| 3469 | else |
| 3470 | { |
| 3471 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3472 | p = *arg; |
| 3473 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3474 | p += 2; |
| 3475 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3476 | ; |
| 3477 | if (*p != '(') |
| 3478 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3479 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3480 | return FAIL; |
| 3481 | } |
| 3482 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3483 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | return FAIL; |
| 3485 | } |
| 3486 | } |
| 3487 | else if (**arg == '[') |
| 3488 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3489 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3490 | type_T **typep; |
| 3491 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3492 | // list index: list[123] |
| 3493 | // list member: dict[key] |
| 3494 | // TODO: blob index |
| 3495 | // TODO: more arguments |
| 3496 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3497 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3498 | return FAIL; |
| 3499 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3500 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3501 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3502 | return FAIL; |
| 3503 | |
| 3504 | if (**arg != ']') |
| 3505 | { |
| 3506 | emsg(_(e_missbrac)); |
| 3507 | return FAIL; |
| 3508 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3509 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3510 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3511 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
| 3512 | if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3513 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3514 | if ((*typep)->tt_type == VAR_LIST) |
| 3515 | *typep = (*typep)->tt_member; |
| 3516 | if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) |
| 3517 | return FAIL; |
| 3518 | } |
| 3519 | else if ((*typep)->tt_type == VAR_DICT) |
| 3520 | { |
| 3521 | *typep = (*typep)->tt_member; |
| 3522 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3523 | return FAIL; |
| 3524 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3525 | return FAIL; |
| 3526 | } |
| 3527 | else |
| 3528 | { |
| 3529 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3530 | return FAIL; |
| 3531 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3532 | } |
| 3533 | else if (**arg == '.' && (*arg)[1] != '.') |
| 3534 | { |
| 3535 | char_u *p; |
| 3536 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3537 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3538 | return FAIL; |
| 3539 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3540 | ++*arg; |
| 3541 | p = *arg; |
| 3542 | // dictionary member: dict.name |
| 3543 | if (eval_isnamec1(*p)) |
| 3544 | while (eval_isnamec(*p)) |
| 3545 | MB_PTR_ADV(p); |
| 3546 | if (p == *arg) |
| 3547 | { |
| 3548 | semsg(_(e_syntax_at), *arg); |
| 3549 | return FAIL; |
| 3550 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3551 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3552 | return FAIL; |
| 3553 | *arg = p; |
| 3554 | } |
| 3555 | else |
| 3556 | break; |
| 3557 | } |
| 3558 | |
| 3559 | // TODO - see handle_subscript(): |
| 3560 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3561 | // Don't do this when "Func" is already a partial that was bound |
| 3562 | // explicitly (pt_auto is FALSE). |
| 3563 | |
| 3564 | return OK; |
| 3565 | } |
| 3566 | |
| 3567 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3568 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3569 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3570 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3571 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3572 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3573 | * |
| 3574 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3575 | */ |
| 3576 | |
| 3577 | /* |
| 3578 | * number number constant |
| 3579 | * 0zFFFFFFFF Blob constant |
| 3580 | * "string" string constant |
| 3581 | * 'string' literal string constant |
| 3582 | * &option-name option value |
| 3583 | * @r register contents |
| 3584 | * identifier variable value |
| 3585 | * function() function call |
| 3586 | * $VAR environment variable |
| 3587 | * (expression) nested expression |
| 3588 | * [expr, expr] List |
| 3589 | * {key: val, key: val} Dictionary |
| 3590 | * #{key: val, key: val} Dictionary with literal keys |
| 3591 | * |
| 3592 | * Also handle: |
| 3593 | * ! in front logical NOT |
| 3594 | * - in front unary minus |
| 3595 | * + in front unary plus (ignored) |
| 3596 | * trailing (arg) funcref/partial call |
| 3597 | * trailing [] subscript in String or List |
| 3598 | * trailing .name entry in Dictionary |
| 3599 | * trailing ->name() method call |
| 3600 | */ |
| 3601 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3602 | compile_expr7( |
| 3603 | char_u **arg, |
| 3604 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3605 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3606 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3607 | char_u *start_leader, *end_leader; |
| 3608 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3609 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3610 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3611 | |
| 3612 | /* |
| 3613 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3614 | */ |
| 3615 | start_leader = *arg; |
| 3616 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3617 | *arg = skipwhite(*arg + 1); |
| 3618 | end_leader = *arg; |
| 3619 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3620 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3621 | switch (**arg) |
| 3622 | { |
| 3623 | /* |
| 3624 | * Number constant. |
| 3625 | */ |
| 3626 | case '0': // also for blob starting with 0z |
| 3627 | case '1': |
| 3628 | case '2': |
| 3629 | case '3': |
| 3630 | case '4': |
| 3631 | case '5': |
| 3632 | case '6': |
| 3633 | case '7': |
| 3634 | case '8': |
| 3635 | case '9': |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3636 | case '.': if (get_number_tv(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3637 | return FAIL; |
| 3638 | break; |
| 3639 | |
| 3640 | /* |
| 3641 | * String constant: "string". |
| 3642 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3643 | case '"': if (get_string_tv(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3644 | return FAIL; |
| 3645 | break; |
| 3646 | |
| 3647 | /* |
| 3648 | * Literal string constant: 'str''ing'. |
| 3649 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3650 | case '\'': if (get_lit_string_tv(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3651 | return FAIL; |
| 3652 | break; |
| 3653 | |
| 3654 | /* |
| 3655 | * Constant Vim variable. |
| 3656 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3657 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3658 | ret = NOTDONE; |
| 3659 | break; |
| 3660 | |
| 3661 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3662 | * "true" constant |
| 3663 | */ |
| 3664 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3665 | && !eval_isnamec((*arg)[4])) |
| 3666 | { |
| 3667 | *arg += 4; |
| 3668 | rettv->v_type = VAR_BOOL; |
| 3669 | rettv->vval.v_number = VVAL_TRUE; |
| 3670 | } |
| 3671 | else |
| 3672 | ret = NOTDONE; |
| 3673 | break; |
| 3674 | |
| 3675 | /* |
| 3676 | * "false" constant |
| 3677 | */ |
| 3678 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3679 | && !eval_isnamec((*arg)[5])) |
| 3680 | { |
| 3681 | *arg += 5; |
| 3682 | rettv->v_type = VAR_BOOL; |
| 3683 | rettv->vval.v_number = VVAL_FALSE; |
| 3684 | } |
| 3685 | else |
| 3686 | ret = NOTDONE; |
| 3687 | break; |
| 3688 | |
| 3689 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3690 | * List: [expr, expr] |
| 3691 | */ |
| 3692 | case '[': ret = compile_list(arg, cctx); |
| 3693 | break; |
| 3694 | |
| 3695 | /* |
| 3696 | * Dictionary: #{key: val, key: val} |
| 3697 | */ |
| 3698 | case '#': if ((*arg)[1] == '{') |
| 3699 | { |
| 3700 | ++*arg; |
| 3701 | ret = compile_dict(arg, cctx, TRUE); |
| 3702 | } |
| 3703 | else |
| 3704 | ret = NOTDONE; |
| 3705 | break; |
| 3706 | |
| 3707 | /* |
| 3708 | * Lambda: {arg, arg -> expr} |
| 3709 | * Dictionary: {'key': val, 'key': val} |
| 3710 | */ |
| 3711 | case '{': { |
| 3712 | char_u *start = skipwhite(*arg + 1); |
| 3713 | |
| 3714 | // Find out what comes after the arguments. |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3715 | // TODO: pass getline function |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3716 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3717 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3718 | if (ret != FAIL && *start == '>') |
| 3719 | ret = compile_lambda(arg, cctx); |
| 3720 | else |
| 3721 | ret = compile_dict(arg, cctx, FALSE); |
| 3722 | } |
| 3723 | break; |
| 3724 | |
| 3725 | /* |
| 3726 | * Option value: &name |
| 3727 | */ |
| 3728 | case '&': ret = compile_get_option(arg, cctx); |
| 3729 | break; |
| 3730 | |
| 3731 | /* |
| 3732 | * Environment variable: $VAR. |
| 3733 | */ |
| 3734 | case '$': ret = compile_get_env(arg, cctx); |
| 3735 | break; |
| 3736 | |
| 3737 | /* |
| 3738 | * Register contents: @r. |
| 3739 | */ |
| 3740 | case '@': ret = compile_get_register(arg, cctx); |
| 3741 | break; |
| 3742 | /* |
| 3743 | * nested expression: (expression). |
| 3744 | */ |
| 3745 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3746 | |
| 3747 | // recursive! |
| 3748 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3749 | { |
| 3750 | ret = compile_expr1(arg, cctx, ppconst); |
| 3751 | } |
| 3752 | else |
| 3753 | { |
| 3754 | // Not enough space in ppconst, flush constants. |
| 3755 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3756 | return FAIL; |
| 3757 | ret = compile_expr0(arg, cctx); |
| 3758 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | *arg = skipwhite(*arg); |
| 3760 | if (**arg == ')') |
| 3761 | ++*arg; |
| 3762 | else if (ret == OK) |
| 3763 | { |
| 3764 | emsg(_(e_missing_close)); |
| 3765 | ret = FAIL; |
| 3766 | } |
| 3767 | break; |
| 3768 | |
| 3769 | default: ret = NOTDONE; |
| 3770 | break; |
| 3771 | } |
| 3772 | if (ret == FAIL) |
| 3773 | return FAIL; |
| 3774 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3775 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | { |
| 3777 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3778 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3779 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3780 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3781 | return FAIL; |
| 3782 | } |
| 3783 | start_leader = end_leader; // don't apply again below |
| 3784 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3785 | if (cctx->ctx_skip == TRUE) |
| 3786 | clear_tv(rettv); |
| 3787 | else |
| 3788 | // A constant expression can possibly be handled compile time, |
| 3789 | // return the value instead of generating code. |
| 3790 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3791 | } |
| 3792 | else if (ret == NOTDONE) |
| 3793 | { |
| 3794 | char_u *p; |
| 3795 | int r; |
| 3796 | |
| 3797 | if (!eval_isnamec1(**arg)) |
| 3798 | { |
| 3799 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3800 | return FAIL; |
| 3801 | } |
| 3802 | |
| 3803 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3804 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3805 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3806 | { |
| 3807 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3808 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3809 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3810 | { |
| 3811 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3812 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3813 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3814 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3815 | if (r == FAIL) |
| 3816 | return FAIL; |
| 3817 | } |
| 3818 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3819 | // Handle following "[]", ".member", etc. |
| 3820 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3821 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3822 | ppconst) == FAIL) |
| 3823 | return FAIL; |
| 3824 | if (ppconst->pp_used > 0) |
| 3825 | { |
| 3826 | // apply the '!', '-' and '+' before the constant |
| 3827 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3828 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 3829 | return FAIL; |
| 3830 | return OK; |
| 3831 | } |
| 3832 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3833 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3834 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | /* |
| 3838 | * * number multiplication |
| 3839 | * / number division |
| 3840 | * % number modulo |
| 3841 | */ |
| 3842 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3843 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3844 | { |
| 3845 | char_u *op; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3846 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3847 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3848 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3849 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3850 | return FAIL; |
| 3851 | |
| 3852 | /* |
| 3853 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3854 | */ |
| 3855 | for (;;) |
| 3856 | { |
| 3857 | op = skipwhite(*arg); |
| 3858 | if (*op != '*' && *op != '/' && *op != '%') |
| 3859 | break; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3860 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3861 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3862 | { |
| 3863 | char_u buf[3]; |
| 3864 | |
| 3865 | vim_strncpy(buf, op, 1); |
| 3866 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3867 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3868 | } |
| 3869 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3870 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3871 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3872 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3873 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3874 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3875 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3876 | |
| 3877 | if (ppconst->pp_used == ppconst_used + 2 |
| 3878 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3879 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3880 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3881 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3882 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3883 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3884 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3885 | // both are numbers: compute the result |
| 3886 | switch (*op) |
| 3887 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3888 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3889 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3890 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3891 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3892 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3893 | break; |
| 3894 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3895 | tv1->vval.v_number = res; |
| 3896 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3897 | } |
| 3898 | else |
| 3899 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3900 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3901 | generate_two_op(cctx, op); |
| 3902 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | return OK; |
| 3906 | } |
| 3907 | |
| 3908 | /* |
| 3909 | * + number addition |
| 3910 | * - number subtraction |
| 3911 | * .. string concatenation |
| 3912 | */ |
| 3913 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3914 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3915 | { |
| 3916 | char_u *op; |
| 3917 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3918 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3919 | |
| 3920 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3921 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3922 | return FAIL; |
| 3923 | |
| 3924 | /* |
| 3925 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3926 | */ |
| 3927 | for (;;) |
| 3928 | { |
| 3929 | op = skipwhite(*arg); |
| 3930 | if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.'))) |
| 3931 | break; |
| 3932 | oplen = (*op == '.' ? 2 : 1); |
| 3933 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3934 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3935 | { |
| 3936 | char_u buf[3]; |
| 3937 | |
| 3938 | vim_strncpy(buf, op, oplen); |
| 3939 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3940 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3944 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3945 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3946 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3947 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3948 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3949 | return FAIL; |
| 3950 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3951 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3952 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3953 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3954 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3955 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3956 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3957 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3958 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3959 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3960 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3961 | // concat/subtract/add constant numbers |
| 3962 | if (*op == '+') |
| 3963 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3964 | else if (*op == '-') |
| 3965 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3966 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3967 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3968 | // concatenate constant strings |
| 3969 | char_u *s1 = tv1->vval.v_string; |
| 3970 | char_u *s2 = tv2->vval.v_string; |
| 3971 | size_t len1 = STRLEN(s1); |
| 3972 | |
| 3973 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3974 | if (tv1->vval.v_string == NULL) |
| 3975 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3976 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3977 | return FAIL; |
| 3978 | } |
| 3979 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3980 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3981 | vim_free(s1); |
| 3982 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3983 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3984 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3985 | } |
| 3986 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3987 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3988 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3989 | if (*op == '.') |
| 3990 | { |
| 3991 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3992 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3993 | return FAIL; |
| 3994 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3995 | } |
| 3996 | else |
| 3997 | generate_two_op(cctx, op); |
| 3998 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | return OK; |
| 4002 | } |
| 4003 | |
| 4004 | /* |
| 4005 | * expr5a == expr5b |
| 4006 | * expr5a =~ expr5b |
| 4007 | * expr5a != expr5b |
| 4008 | * expr5a !~ expr5b |
| 4009 | * expr5a > expr5b |
| 4010 | * expr5a >= expr5b |
| 4011 | * expr5a < expr5b |
| 4012 | * expr5a <= expr5b |
| 4013 | * expr5a is expr5b |
| 4014 | * expr5a isnot expr5b |
| 4015 | * |
| 4016 | * Produces instructions: |
| 4017 | * EVAL expr5a Push result of "expr5a" |
| 4018 | * EVAL expr5b Push result of "expr5b" |
| 4019 | * COMPARE one of the compare instructions |
| 4020 | */ |
| 4021 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4022 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4023 | { |
| 4024 | exptype_T type = EXPR_UNKNOWN; |
| 4025 | char_u *p; |
| 4026 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4027 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4028 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4029 | |
| 4030 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4031 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4032 | return FAIL; |
| 4033 | |
| 4034 | p = skipwhite(*arg); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4035 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4036 | |
| 4037 | /* |
| 4038 | * If there is a comparative operator, use it. |
| 4039 | */ |
| 4040 | if (type != EXPR_UNKNOWN) |
| 4041 | { |
| 4042 | int ic = FALSE; // Default: do not ignore case |
| 4043 | |
| 4044 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4045 | { |
| 4046 | semsg(_(e_invexpr2), *arg); |
| 4047 | return FAIL; |
| 4048 | } |
| 4049 | // extra question mark appended: ignore case |
| 4050 | if (p[len] == '?') |
| 4051 | { |
| 4052 | ic = TRUE; |
| 4053 | ++len; |
| 4054 | } |
| 4055 | // extra '#' appended: match case (ignored) |
| 4056 | else if (p[len] == '#') |
| 4057 | ++len; |
| 4058 | // nothing appended: match case |
| 4059 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4060 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4061 | { |
| 4062 | char_u buf[7]; |
| 4063 | |
| 4064 | vim_strncpy(buf, p, len); |
| 4065 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4066 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | // get the second variable |
| 4070 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4071 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4072 | return FAIL; |
| 4073 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4074 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4075 | return FAIL; |
| 4076 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4077 | if (ppconst->pp_used == ppconst_used + 2) |
| 4078 | { |
| 4079 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4080 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4081 | int ret; |
| 4082 | |
| 4083 | // Both sides are a constant, compute the result now. |
| 4084 | // First check for a valid combination of types, this is more |
| 4085 | // strict than typval_compare(). |
| 4086 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 4087 | ret = FAIL; |
| 4088 | else |
| 4089 | { |
| 4090 | ret = typval_compare(tv1, tv2, type, ic); |
| 4091 | tv1->v_type = VAR_BOOL; |
| 4092 | tv1->vval.v_number = tv1->vval.v_number |
| 4093 | ? VVAL_TRUE : VVAL_FALSE; |
| 4094 | clear_tv(tv2); |
| 4095 | --ppconst->pp_used; |
| 4096 | } |
| 4097 | return ret; |
| 4098 | } |
| 4099 | |
| 4100 | generate_ppconst(cctx, ppconst); |
| 4101 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | return OK; |
| 4105 | } |
| 4106 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4107 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4108 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4109 | /* |
| 4110 | * Compile || or &&. |
| 4111 | */ |
| 4112 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4113 | compile_and_or( |
| 4114 | char_u **arg, |
| 4115 | cctx_T *cctx, |
| 4116 | char *op, |
| 4117 | ppconst_T *ppconst, |
| 4118 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4119 | { |
| 4120 | char_u *p = skipwhite(*arg); |
| 4121 | int opchar = *op; |
| 4122 | |
| 4123 | if (p[0] == opchar && p[1] == opchar) |
| 4124 | { |
| 4125 | garray_T *instr = &cctx->ctx_instr; |
| 4126 | garray_T end_ga; |
| 4127 | |
| 4128 | /* |
| 4129 | * Repeat until there is no following "||" or "&&" |
| 4130 | */ |
| 4131 | ga_init2(&end_ga, sizeof(int), 10); |
| 4132 | while (p[0] == opchar && p[1] == opchar) |
| 4133 | { |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4134 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4135 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4136 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4137 | return FAIL; |
| 4138 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4139 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4140 | // TODO: use ppconst if the value is a constant |
| 4141 | generate_ppconst(cctx, ppconst); |
| 4142 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4143 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4144 | { |
| 4145 | ga_clear(&end_ga); |
| 4146 | return FAIL; |
| 4147 | } |
| 4148 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4149 | ++end_ga.ga_len; |
| 4150 | generate_JUMP(cctx, opchar == '|' |
| 4151 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4152 | |
| 4153 | // eval the next expression |
| 4154 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4155 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4156 | return FAIL; |
| 4157 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4158 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4159 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4160 | { |
| 4161 | ga_clear(&end_ga); |
| 4162 | return FAIL; |
| 4163 | } |
| 4164 | p = skipwhite(*arg); |
| 4165 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4166 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4167 | |
| 4168 | // Fill in the end label in all jumps. |
| 4169 | while (end_ga.ga_len > 0) |
| 4170 | { |
| 4171 | isn_T *isn; |
| 4172 | |
| 4173 | --end_ga.ga_len; |
| 4174 | isn = ((isn_T *)instr->ga_data) |
| 4175 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4176 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4177 | } |
| 4178 | ga_clear(&end_ga); |
| 4179 | } |
| 4180 | |
| 4181 | return OK; |
| 4182 | } |
| 4183 | |
| 4184 | /* |
| 4185 | * expr4a && expr4a && expr4a logical AND |
| 4186 | * |
| 4187 | * Produces instructions: |
| 4188 | * EVAL expr4a Push result of "expr4a" |
| 4189 | * JUMP_AND_KEEP_IF_FALSE end |
| 4190 | * EVAL expr4b Push result of "expr4b" |
| 4191 | * JUMP_AND_KEEP_IF_FALSE end |
| 4192 | * EVAL expr4c Push result of "expr4c" |
| 4193 | * end: |
| 4194 | */ |
| 4195 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4196 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4197 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4198 | int ppconst_used = ppconst->pp_used; |
| 4199 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4200 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4201 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4202 | return FAIL; |
| 4203 | |
| 4204 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4205 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | /* |
| 4209 | * expr3a || expr3b || expr3c logical OR |
| 4210 | * |
| 4211 | * Produces instructions: |
| 4212 | * EVAL expr3a Push result of "expr3a" |
| 4213 | * JUMP_AND_KEEP_IF_TRUE end |
| 4214 | * EVAL expr3b Push result of "expr3b" |
| 4215 | * JUMP_AND_KEEP_IF_TRUE end |
| 4216 | * EVAL expr3c Push result of "expr3c" |
| 4217 | * end: |
| 4218 | */ |
| 4219 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4220 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4221 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4222 | int ppconst_used = ppconst->pp_used; |
| 4223 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4224 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4225 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | return FAIL; |
| 4227 | |
| 4228 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4229 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | /* |
| 4233 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4234 | * |
| 4235 | * Produces instructions: |
| 4236 | * EVAL expr2 Push result of "expr" |
| 4237 | * JUMP_IF_FALSE alt jump if false |
| 4238 | * EVAL expr1a |
| 4239 | * JUMP_ALWAYS end |
| 4240 | * alt: EVAL expr1b |
| 4241 | * end: |
| 4242 | */ |
| 4243 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4244 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4245 | { |
| 4246 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4247 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4248 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4249 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4250 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4251 | return FAIL; |
| 4252 | |
| 4253 | p = skipwhite(*arg); |
| 4254 | if (*p == '?') |
| 4255 | { |
| 4256 | garray_T *instr = &cctx->ctx_instr; |
| 4257 | garray_T *stack = &cctx->ctx_type_stack; |
| 4258 | int alt_idx = instr->ga_len; |
| 4259 | int end_idx; |
| 4260 | isn_T *isn; |
| 4261 | type_T *type1; |
| 4262 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4263 | int has_const_expr = FALSE; |
| 4264 | int const_value = FALSE; |
| 4265 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4266 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4267 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4268 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4269 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4270 | return FAIL; |
| 4271 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4272 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4273 | if (ppconst->pp_used == ppconst_used + 1) |
| 4274 | { |
| 4275 | // the condition is a constant, we know whether the ? or the : |
| 4276 | // expression is to be evaluated. |
| 4277 | has_const_expr = TRUE; |
| 4278 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4279 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4280 | --ppconst->pp_used; |
| 4281 | cctx->ctx_skip = save_skip == TRUE || !const_value; |
| 4282 | } |
| 4283 | else |
| 4284 | { |
| 4285 | generate_ppconst(cctx, ppconst); |
| 4286 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4287 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | |
| 4289 | // evaluate the second expression; any type is accepted |
| 4290 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4291 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4292 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4293 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4294 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4295 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4296 | if (!has_const_expr) |
| 4297 | { |
| 4298 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4299 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4300 | // remember the type and drop it |
| 4301 | --stack->ga_len; |
| 4302 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4303 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4304 | end_idx = instr->ga_len; |
| 4305 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4306 | |
| 4307 | // jump here from JUMP_IF_FALSE |
| 4308 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4309 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4310 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4311 | |
| 4312 | // Check for the ":". |
| 4313 | p = skipwhite(*arg); |
| 4314 | if (*p != ':') |
| 4315 | { |
| 4316 | emsg(_(e_missing_colon)); |
| 4317 | return FAIL; |
| 4318 | } |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4319 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4320 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4321 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4322 | return FAIL; |
| 4323 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | |
| 4325 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4326 | if (has_const_expr) |
| 4327 | cctx->ctx_skip = save_skip == TRUE || const_value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4328 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4329 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4330 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4331 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4332 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4333 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4334 | if (!has_const_expr) |
| 4335 | { |
| 4336 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4337 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4338 | // If the types differ, the result has a more generic type. |
| 4339 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4340 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4341 | |
| 4342 | // jump here from JUMP_ALWAYS |
| 4343 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4344 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4345 | } |
| 4346 | |
| 4347 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4348 | } |
| 4349 | return OK; |
| 4350 | } |
| 4351 | |
| 4352 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4353 | * Toplevel expression. |
| 4354 | */ |
| 4355 | static int |
| 4356 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4357 | { |
| 4358 | ppconst_T ppconst; |
| 4359 | |
| 4360 | CLEAR_FIELD(ppconst); |
| 4361 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4362 | { |
| 4363 | clear_ppconst(&ppconst); |
| 4364 | return FAIL; |
| 4365 | } |
| 4366 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4367 | return FAIL; |
| 4368 | return OK; |
| 4369 | } |
| 4370 | |
| 4371 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4372 | * compile "return [expr]" |
| 4373 | */ |
| 4374 | static char_u * |
| 4375 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4376 | { |
| 4377 | char_u *p = arg; |
| 4378 | garray_T *stack = &cctx->ctx_type_stack; |
| 4379 | type_T *stack_type; |
| 4380 | |
| 4381 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4382 | { |
| 4383 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4384 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4385 | return NULL; |
| 4386 | |
| 4387 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4388 | if (set_return_type) |
| 4389 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
| 4390 | else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx) |
| 4391 | == FAIL) |
| 4392 | return NULL; |
| 4393 | } |
| 4394 | else |
| 4395 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4396 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4397 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4398 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4399 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4400 | { |
| 4401 | emsg(_("E1003: Missing return value")); |
| 4402 | return NULL; |
| 4403 | } |
| 4404 | |
| 4405 | // No argument, return zero. |
| 4406 | generate_PUSHNR(cctx, 0); |
| 4407 | } |
| 4408 | |
| 4409 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4410 | return NULL; |
| 4411 | |
| 4412 | // "return val | endif" is possible |
| 4413 | return skipwhite(p); |
| 4414 | } |
| 4415 | |
| 4416 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4417 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4418 | * Return a pointer to the line in allocated memory. |
| 4419 | * Return NULL for end-of-file or some error. |
| 4420 | */ |
| 4421 | static char_u * |
| 4422 | exarg_getline( |
| 4423 | int c UNUSED, |
| 4424 | void *cookie, |
| 4425 | int indent UNUSED, |
| 4426 | int do_concat UNUSED) |
| 4427 | { |
| 4428 | cctx_T *cctx = (cctx_T *)cookie; |
| 4429 | |
| 4430 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4431 | { |
| 4432 | iemsg("Heredoc got to end"); |
| 4433 | return NULL; |
| 4434 | } |
| 4435 | ++cctx->ctx_lnum; |
| 4436 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4437 | [cctx->ctx_lnum]); |
| 4438 | } |
| 4439 | |
| 4440 | /* |
| 4441 | * Compile a nested :def command. |
| 4442 | */ |
| 4443 | static char_u * |
| 4444 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4445 | { |
| 4446 | char_u *name_start = eap->arg; |
| 4447 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4448 | char_u *name = get_lambda_name(); |
| 4449 | lvar_T *lvar; |
| 4450 | ufunc_T *ufunc; |
| 4451 | |
| 4452 | eap->arg = name_end; |
| 4453 | eap->getline = exarg_getline; |
| 4454 | eap->cookie = cctx; |
| 4455 | eap->skip = cctx->ctx_skip == TRUE; |
| 4456 | eap->forceit = FALSE; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 4457 | ufunc = def_function(eap, name, cctx, TRUE); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4458 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4459 | if (ufunc == NULL || ufunc->uf_dfunc_idx < 0) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4460 | return NULL; |
| 4461 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4462 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4463 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4464 | TRUE, ufunc->uf_func_type); |
| 4465 | |
| 4466 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4467 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4468 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4469 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4470 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4471 | return (char_u *)""; |
| 4472 | } |
| 4473 | |
| 4474 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4475 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4476 | */ |
| 4477 | int |
| 4478 | assignment_len(char_u *p, int *heredoc) |
| 4479 | { |
| 4480 | if (*p == '=') |
| 4481 | { |
| 4482 | if (p[1] == '<' && p[2] == '<') |
| 4483 | { |
| 4484 | *heredoc = TRUE; |
| 4485 | return 3; |
| 4486 | } |
| 4487 | return 1; |
| 4488 | } |
| 4489 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4490 | return 2; |
| 4491 | if (STRNCMP(p, "..=", 3) == 0) |
| 4492 | return 3; |
| 4493 | return 0; |
| 4494 | } |
| 4495 | |
| 4496 | // words that cannot be used as a variable |
| 4497 | static char *reserved[] = { |
| 4498 | "true", |
| 4499 | "false", |
| 4500 | NULL |
| 4501 | }; |
| 4502 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4503 | typedef enum { |
| 4504 | dest_local, |
| 4505 | dest_option, |
| 4506 | dest_env, |
| 4507 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4508 | dest_buffer, |
| 4509 | dest_window, |
| 4510 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4511 | dest_vimvar, |
| 4512 | dest_script, |
| 4513 | dest_reg, |
| 4514 | } assign_dest_T; |
| 4515 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4516 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4517 | * Generate the load instruction for "name". |
| 4518 | */ |
| 4519 | static void |
| 4520 | generate_loadvar( |
| 4521 | cctx_T *cctx, |
| 4522 | assign_dest_T dest, |
| 4523 | char_u *name, |
| 4524 | lvar_T *lvar, |
| 4525 | type_T *type) |
| 4526 | { |
| 4527 | switch (dest) |
| 4528 | { |
| 4529 | case dest_option: |
| 4530 | // TODO: check the option exists |
| 4531 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4532 | break; |
| 4533 | case dest_global: |
| 4534 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4535 | break; |
| 4536 | case dest_buffer: |
| 4537 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4538 | break; |
| 4539 | case dest_window: |
| 4540 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4541 | break; |
| 4542 | case dest_tab: |
| 4543 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4544 | break; |
| 4545 | case dest_script: |
| 4546 | compile_load_scriptvar(cctx, |
| 4547 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4548 | break; |
| 4549 | case dest_env: |
| 4550 | // Include $ in the name here |
| 4551 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4552 | break; |
| 4553 | case dest_reg: |
| 4554 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4555 | break; |
| 4556 | case dest_vimvar: |
| 4557 | generate_LOADV(cctx, name + 2, TRUE); |
| 4558 | break; |
| 4559 | case dest_local: |
| 4560 | if (lvar->lv_from_outer) |
| 4561 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4562 | NULL, type); |
| 4563 | else |
| 4564 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4565 | break; |
| 4566 | } |
| 4567 | } |
| 4568 | |
| 4569 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4570 | * compile "let var [= expr]", "const var = expr" and "var = expr" |
| 4571 | * "arg" points to "var". |
| 4572 | */ |
| 4573 | static char_u * |
| 4574 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4575 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4576 | char_u *var_end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4577 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4578 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4579 | char_u *ret = NULL; |
| 4580 | int var_count = 0; |
| 4581 | int semicolon = 0; |
| 4582 | size_t varlen; |
| 4583 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4584 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4585 | int new_local = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4586 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4587 | int opt_type; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4588 | assign_dest_T dest = dest_local; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4589 | int opt_flags = 0; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 4590 | int vimvaridx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4591 | int oplen = 0; |
| 4592 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4593 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4594 | type_T *member_type = &t_any; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4595 | lvar_T *lvar = NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4596 | lvar_T arg_lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4597 | char_u *name; |
| 4598 | char_u *sp; |
| 4599 | int has_type = FALSE; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4600 | int has_index = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4601 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
| 4602 | int instr_count = -1; |
| 4603 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4604 | var_end = skip_var_list(arg, FALSE, &var_count, &semicolon); |
| 4605 | if (var_end == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4606 | return NULL; |
| 4607 | if (var_count > 0) |
| 4608 | { |
| 4609 | // TODO: let [var, var] = list |
| 4610 | emsg("Cannot handle a list yet"); |
| 4611 | return NULL; |
| 4612 | } |
| 4613 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4614 | p = (*arg == '&' || *arg == '$' || *arg == '@') ? arg + 1 : arg; |
| 4615 | p = to_name_end(p, TRUE); |
| 4616 | |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4617 | // "a: type" is declaring variable "a" with a type, not "a:". |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4618 | if (is_decl && var_end == arg + 2 && var_end[-1] == ':') |
| 4619 | --var_end; |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4620 | if (is_decl && p == arg + 2 && p[-1] == ':') |
| 4621 | --p; |
| 4622 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4623 | varlen = p - arg; |
| 4624 | name = vim_strnsave(arg, (int)varlen); |
| 4625 | if (name == NULL) |
| 4626 | return NULL; |
| 4627 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4628 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4629 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4630 | if (*arg == '&') |
| 4631 | { |
| 4632 | int cc; |
| 4633 | long numval; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4634 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4635 | dest = dest_option; |
| 4636 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4637 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4638 | emsg(_(e_const_option)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4639 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4640 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4641 | if (is_decl) |
| 4642 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4643 | semsg(_("E1052: Cannot declare an option: %s"), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4644 | goto theend; |
| 4645 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4646 | p = arg; |
| 4647 | p = find_option_end(&p, &opt_flags); |
| 4648 | if (p == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4649 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4650 | // cannot happen? |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4651 | emsg(_(e_letunexp)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4652 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4653 | } |
| 4654 | cc = *p; |
| 4655 | *p = NUL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4656 | opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4657 | *p = cc; |
| 4658 | if (opt_type == -3) |
| 4659 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4660 | semsg(_(e_unknown_option), arg); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4661 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4662 | } |
| 4663 | if (opt_type == -2 || opt_type == 0) |
| 4664 | type = &t_string; |
| 4665 | else |
| 4666 | type = &t_number; // both number and boolean option |
| 4667 | } |
| 4668 | else if (*arg == '$') |
| 4669 | { |
| 4670 | dest = dest_env; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4671 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4672 | if (is_decl) |
| 4673 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4674 | semsg(_("E1065: Cannot declare an environment variable: %s"), |
| 4675 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4676 | goto theend; |
| 4677 | } |
| 4678 | } |
| 4679 | else if (*arg == '@') |
| 4680 | { |
| 4681 | if (!valid_yank_reg(arg[1], TRUE)) |
| 4682 | { |
| 4683 | emsg_invreg(arg[1]); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4684 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4685 | } |
| 4686 | dest = dest_reg; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4687 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4688 | if (is_decl) |
| 4689 | { |
| 4690 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 4691 | goto theend; |
| 4692 | } |
| 4693 | } |
| 4694 | else if (STRNCMP(arg, "g:", 2) == 0) |
| 4695 | { |
| 4696 | dest = dest_global; |
| 4697 | if (is_decl) |
| 4698 | { |
| 4699 | semsg(_("E1016: Cannot declare a global variable: %s"), name); |
| 4700 | goto theend; |
| 4701 | } |
| 4702 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4703 | else if (STRNCMP(arg, "b:", 2) == 0) |
| 4704 | { |
| 4705 | dest = dest_buffer; |
| 4706 | if (is_decl) |
| 4707 | { |
| 4708 | semsg(_("E1078: Cannot declare a buffer variable: %s"), name); |
| 4709 | goto theend; |
| 4710 | } |
| 4711 | } |
| 4712 | else if (STRNCMP(arg, "w:", 2) == 0) |
| 4713 | { |
| 4714 | dest = dest_window; |
| 4715 | if (is_decl) |
| 4716 | { |
| 4717 | semsg(_("E1079: Cannot declare a window variable: %s"), name); |
| 4718 | goto theend; |
| 4719 | } |
| 4720 | } |
| 4721 | else if (STRNCMP(arg, "t:", 2) == 0) |
| 4722 | { |
| 4723 | dest = dest_tab; |
| 4724 | if (is_decl) |
| 4725 | { |
| 4726 | semsg(_("E1080: Cannot declare a tab variable: %s"), name); |
| 4727 | goto theend; |
| 4728 | } |
| 4729 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4730 | else if (STRNCMP(arg, "v:", 2) == 0) |
| 4731 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4732 | typval_T *vtv; |
| 4733 | int di_flags; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4734 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4735 | vimvaridx = find_vim_var(name + 2, &di_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4736 | if (vimvaridx < 0) |
| 4737 | { |
| 4738 | semsg(_(e_var_notfound), arg); |
| 4739 | goto theend; |
| 4740 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4741 | // We use the current value of "sandbox" here, is that OK? |
| 4742 | if (var_check_ro(di_flags, name, FALSE)) |
| 4743 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4744 | dest = dest_vimvar; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4745 | vtv = get_vim_var_tv(vimvaridx); |
| 4746 | type = typval2type(vtv); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4747 | if (is_decl) |
| 4748 | { |
| 4749 | semsg(_("E1064: Cannot declare a v: variable: %s"), name); |
| 4750 | goto theend; |
| 4751 | } |
| 4752 | } |
| 4753 | else |
| 4754 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4755 | int idx; |
| 4756 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4757 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4758 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4759 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4760 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4761 | goto theend; |
| 4762 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4763 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4764 | lvar = lookup_local(arg, varlen, cctx); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4765 | if (lvar == NULL |
| 4766 | && lookup_arg(arg, varlen, |
| 4767 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4768 | &arg_lvar.lv_from_outer, cctx) == OK) |
| 4769 | { |
| 4770 | if (is_decl) |
| 4771 | { |
| 4772 | semsg(_(e_used_as_arg), name); |
| 4773 | goto theend; |
| 4774 | } |
| 4775 | arg_lvar.lv_const = 0; |
| 4776 | lvar = &arg_lvar; |
| 4777 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4778 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4779 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4780 | if (is_decl) |
| 4781 | { |
| 4782 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4783 | goto theend; |
| 4784 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4785 | else if (lvar->lv_const) |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4786 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4787 | semsg(_("E1018: Cannot assign to a constant: %s"), name); |
| 4788 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4789 | } |
| 4790 | } |
| 4791 | else if (STRNCMP(arg, "s:", 2) == 0 |
| 4792 | || lookup_script(arg, varlen) == OK |
| 4793 | || find_imported(arg, varlen, cctx) != NULL) |
| 4794 | { |
| 4795 | dest = dest_script; |
| 4796 | if (is_decl) |
| 4797 | { |
| 4798 | semsg(_("E1054: Variable already declared in the script: %s"), |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4799 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4800 | goto theend; |
| 4801 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4802 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 4803 | else if (name[1] == ':' && name[2] != NUL) |
| 4804 | { |
| 4805 | semsg(_("E1082: Cannot use a namespaced variable: %s"), name); |
| 4806 | goto theend; |
| 4807 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4808 | else if (!is_decl) |
| 4809 | { |
| 4810 | semsg(_("E1089: unknown variable: %s"), name); |
| 4811 | goto theend; |
| 4812 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4813 | } |
| 4814 | } |
| 4815 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4816 | // handle "a:name" as a name, not index "name" on "a" |
| 4817 | if (varlen > 1 || arg[varlen] != ':') |
| 4818 | p = var_end; |
| 4819 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4820 | if (dest != dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4821 | { |
| 4822 | if (is_decl && *p == ':') |
| 4823 | { |
| 4824 | // parse optional type: "let var: type = expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 4825 | if (!VIM_ISWHITE(p[1])) |
| 4826 | { |
| 4827 | semsg(_(e_white_after), ":"); |
| 4828 | goto theend; |
| 4829 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4830 | p = skipwhite(p + 1); |
| 4831 | type = parse_type(&p, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4832 | has_type = TRUE; |
| 4833 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4834 | else if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4835 | type = lvar->lv_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | sp = p; |
| 4839 | p = skipwhite(p); |
| 4840 | op = p; |
| 4841 | oplen = assignment_len(p, &heredoc); |
| 4842 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4843 | { |
| 4844 | char_u buf[4]; |
| 4845 | |
| 4846 | vim_strncpy(buf, op, oplen); |
| 4847 | semsg(_(e_white_both), buf); |
| 4848 | } |
| 4849 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4850 | if (oplen == 3 && !heredoc && dest != dest_global |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4851 | && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4852 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 4853 | emsg(_("E1019: Can only concatenate to string")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4854 | goto theend; |
| 4855 | } |
| 4856 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4857 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4858 | { |
| 4859 | if (oplen > 1 && !heredoc) |
| 4860 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4861 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4862 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4863 | name); |
| 4864 | goto theend; |
| 4865 | } |
| 4866 | |
| 4867 | // new local variable |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 4868 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4869 | goto theend; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4870 | lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type); |
| 4871 | if (lvar == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4872 | goto theend; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4873 | new_local = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4874 | } |
| 4875 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4876 | member_type = type; |
| 4877 | if (var_end > arg + varlen) |
| 4878 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4879 | // Something follows after the variable: "var[idx]". |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4880 | if (is_decl) |
| 4881 | { |
| 4882 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 4883 | goto theend; |
| 4884 | } |
| 4885 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4886 | if (arg[varlen] == '[') |
| 4887 | { |
| 4888 | has_index = TRUE; |
| 4889 | if (type->tt_member == NULL) |
| 4890 | { |
| 4891 | semsg(_("E1088: cannot use an index on %s"), name); |
| 4892 | goto theend; |
| 4893 | } |
| 4894 | member_type = type->tt_member; |
| 4895 | } |
| 4896 | else |
| 4897 | { |
| 4898 | semsg("Not supported yet: %s", arg); |
| 4899 | goto theend; |
| 4900 | } |
| 4901 | } |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4902 | else if (lvar == &arg_lvar) |
| 4903 | { |
| 4904 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 4905 | goto theend; |
| 4906 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4907 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4908 | if (heredoc) |
| 4909 | { |
| 4910 | list_T *l; |
| 4911 | listitem_T *li; |
| 4912 | |
| 4913 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4914 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4915 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4916 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4917 | |
| 4918 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4919 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4920 | { |
| 4921 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4922 | li->li_tv.vval.v_string = NULL; |
| 4923 | } |
| 4924 | generate_NEWLIST(cctx, l->lv_len); |
| 4925 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4926 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4927 | list_free(l); |
| 4928 | p += STRLEN(p); |
| 4929 | } |
| 4930 | else if (oplen > 0) |
| 4931 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4932 | int r; |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4933 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4934 | // for "+=", "*=", "..=" etc. first load the current value |
| 4935 | if (*op != '=') |
| 4936 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4937 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4938 | |
| 4939 | if (has_index) |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4940 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4941 | // TODO: get member from list or dict |
| 4942 | emsg("Index with operation not supported yet"); |
| 4943 | goto theend; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4944 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4947 | // Compile the expression. Temporarily hide the new local variable |
| 4948 | // here, it is not available to this expression. |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4949 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4950 | --cctx->ctx_locals.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4951 | instr_count = instr->ga_len; |
| 4952 | p = skipwhite(p + oplen); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4953 | r = compile_expr0(&p, cctx); |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4954 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4955 | ++cctx->ctx_locals.ga_len; |
| 4956 | if (r == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4957 | goto theend; |
| 4958 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4959 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4960 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4961 | type_T *stacktype; |
| 4962 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4963 | stacktype = stack->ga_len == 0 ? &t_void |
| 4964 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4965 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4966 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4967 | if (new_local && !has_type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4968 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4969 | if (stacktype->tt_type == VAR_VOID) |
| 4970 | { |
| 4971 | emsg(_("E1031: Cannot use void value")); |
| 4972 | goto theend; |
| 4973 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4974 | else |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4975 | { |
| 4976 | // An empty list or dict has a &t_void member, for a |
| 4977 | // variable that implies &t_any. |
| 4978 | if (stacktype == &t_list_empty) |
| 4979 | lvar->lv_type = &t_list_any; |
| 4980 | else if (stacktype == &t_dict_empty) |
| 4981 | lvar->lv_type = &t_dict_any; |
| 4982 | else |
| 4983 | lvar->lv_type = stacktype; |
| 4984 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4985 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4986 | else |
| 4987 | { |
| 4988 | type_T *use_type = lvar->lv_type; |
| 4989 | |
| 4990 | if (has_index) |
| 4991 | { |
| 4992 | use_type = use_type->tt_member; |
| 4993 | if (use_type == NULL) |
| 4994 | use_type = &t_void; |
| 4995 | } |
| 4996 | if (need_type(stacktype, use_type, -1, cctx) == FAIL) |
| 4997 | goto theend; |
| 4998 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4999 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5000 | else if (*p != '=' && check_type(member_type, stacktype, TRUE) |
| 5001 | == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5002 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5003 | } |
| 5004 | } |
| 5005 | else if (cmdidx == CMD_const) |
| 5006 | { |
| 5007 | emsg(_("E1021: const requires a value")); |
| 5008 | goto theend; |
| 5009 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5010 | else if (!has_type || dest == dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5011 | { |
| 5012 | emsg(_("E1022: type or initialization required")); |
| 5013 | goto theend; |
| 5014 | } |
| 5015 | else |
| 5016 | { |
| 5017 | // variables are always initialized |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5018 | if (ga_grow(instr, 1) == FAIL) |
| 5019 | goto theend; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5020 | switch (member_type->tt_type) |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5021 | { |
| 5022 | case VAR_BOOL: |
| 5023 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5024 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5025 | case VAR_FLOAT: |
| 5026 | #ifdef FEAT_FLOAT |
| 5027 | generate_PUSHF(cctx, 0.0); |
| 5028 | #endif |
| 5029 | break; |
| 5030 | case VAR_STRING: |
| 5031 | generate_PUSHS(cctx, NULL); |
| 5032 | break; |
| 5033 | case VAR_BLOB: |
| 5034 | generate_PUSHBLOB(cctx, NULL); |
| 5035 | break; |
| 5036 | case VAR_FUNC: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5037 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5038 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5039 | case VAR_LIST: |
| 5040 | generate_NEWLIST(cctx, 0); |
| 5041 | break; |
| 5042 | case VAR_DICT: |
| 5043 | generate_NEWDICT(cctx, 0); |
| 5044 | break; |
| 5045 | case VAR_JOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 5046 | generate_PUSHJOB(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5047 | break; |
| 5048 | case VAR_CHANNEL: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 5049 | generate_PUSHCHANNEL(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5050 | break; |
| 5051 | case VAR_NUMBER: |
| 5052 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5053 | case VAR_ANY: |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 5054 | case VAR_PARTIAL: |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5055 | case VAR_VOID: |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 5056 | case VAR_SPECIAL: // cannot happen |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 5057 | generate_PUSHNR(cctx, 0); |
| 5058 | break; |
| 5059 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5060 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5061 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5062 | |
| 5063 | if (oplen > 0 && *op != '=') |
| 5064 | { |
| 5065 | type_T *expected = &t_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5066 | type_T *stacktype; |
| 5067 | |
| 5068 | // TODO: if type is known use float or any operation |
| 5069 | |
| 5070 | if (*op == '.') |
| 5071 | expected = &t_string; |
| 5072 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5073 | if (need_type(stacktype, expected, -1, cctx) == FAIL) |
| 5074 | goto theend; |
| 5075 | |
| 5076 | if (*op == '.') |
| 5077 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5078 | else |
| 5079 | { |
| 5080 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5081 | |
| 5082 | if (isn == NULL) |
| 5083 | goto theend; |
| 5084 | switch (*op) |
| 5085 | { |
| 5086 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5087 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5088 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5089 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5090 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5091 | } |
| 5092 | } |
| 5093 | } |
| 5094 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5095 | if (has_index) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5096 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5097 | int r; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 5098 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5099 | // Compile the "idx" in "var[idx]". |
| 5100 | if (new_local) |
| 5101 | --cctx->ctx_locals.ga_len; |
| 5102 | p = skipwhite(arg + varlen + 1); |
| 5103 | r = compile_expr0(&p, cctx); |
| 5104 | if (new_local) |
| 5105 | ++cctx->ctx_locals.ga_len; |
| 5106 | if (r == FAIL) |
| 5107 | goto theend; |
| 5108 | if (*skipwhite(p) != ']') |
| 5109 | { |
| 5110 | emsg(_(e_missbrac)); |
| 5111 | goto theend; |
| 5112 | } |
| 5113 | if (type->tt_type == VAR_DICT |
| 5114 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5115 | goto theend; |
| 5116 | if (type->tt_type == VAR_LIST |
| 5117 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
| 5118 | != VAR_NUMBER) |
| 5119 | { |
| 5120 | emsg(_(e_number_exp)); |
| 5121 | goto theend; |
| 5122 | } |
| 5123 | |
| 5124 | // Load the dict or list. On the stack we then have: |
| 5125 | // - value |
| 5126 | // - index |
| 5127 | // - variable |
| 5128 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5129 | |
| 5130 | if (type->tt_type == VAR_LIST) |
| 5131 | { |
| 5132 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5133 | return FAIL; |
| 5134 | } |
| 5135 | else if (type->tt_type == VAR_DICT) |
| 5136 | { |
| 5137 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5138 | return FAIL; |
| 5139 | } |
| 5140 | else |
| 5141 | { |
| 5142 | emsg(_(e_listreq)); |
| 5143 | goto theend; |
| 5144 | } |
| 5145 | } |
| 5146 | else |
| 5147 | { |
| 5148 | switch (dest) |
| 5149 | { |
| 5150 | case dest_option: |
| 5151 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5152 | break; |
| 5153 | case dest_global: |
| 5154 | // include g: with the name, easier to execute that way |
| 5155 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5156 | break; |
| 5157 | case dest_buffer: |
| 5158 | // include b: with the name, easier to execute that way |
| 5159 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5160 | break; |
| 5161 | case dest_window: |
| 5162 | // include w: with the name, easier to execute that way |
| 5163 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5164 | break; |
| 5165 | case dest_tab: |
| 5166 | // include t: with the name, easier to execute that way |
| 5167 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5168 | break; |
| 5169 | case dest_env: |
| 5170 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5171 | break; |
| 5172 | case dest_reg: |
| 5173 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5174 | break; |
| 5175 | case dest_vimvar: |
| 5176 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5177 | break; |
| 5178 | case dest_script: |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5179 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5180 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5181 | imported_T *import = NULL; |
| 5182 | int sid = current_sctx.sc_sid; |
| 5183 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5184 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5185 | if (name[1] != ':') |
| 5186 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5187 | import = find_imported(name, 0, cctx); |
| 5188 | if (import != NULL) |
| 5189 | sid = import->imp_sid; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5190 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5191 | |
| 5192 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5193 | // TODO: specific type |
| 5194 | if (idx < 0) |
| 5195 | { |
| 5196 | char_u *name_s = name; |
| 5197 | |
| 5198 | // Include s: in the name for store_var() |
| 5199 | if (name[1] != ':') |
| 5200 | { |
| 5201 | int len = (int)STRLEN(name) + 3; |
| 5202 | |
| 5203 | name_s = alloc(len); |
| 5204 | if (name_s == NULL) |
| 5205 | name_s = name; |
| 5206 | else |
| 5207 | vim_snprintf((char *)name_s, len, "s:%s", name); |
| 5208 | } |
| 5209 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
| 5210 | &t_any); |
| 5211 | if (name_s != name) |
| 5212 | vim_free(name_s); |
| 5213 | } |
| 5214 | else |
| 5215 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5216 | sid, idx, &t_any); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5217 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5218 | break; |
| 5219 | case dest_local: |
| 5220 | if (lvar != NULL) |
| 5221 | { |
| 5222 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 5223 | |
| 5224 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5225 | // ISN_STORE into ISN_STORENR |
| 5226 | if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1 |
| 5227 | && isn->isn_type == ISN_PUSHNR) |
| 5228 | { |
| 5229 | varnumber_T val = isn->isn_arg.number; |
| 5230 | |
| 5231 | isn->isn_type = ISN_STORENR; |
| 5232 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5233 | isn->isn_arg.storenr.stnr_val = val; |
| 5234 | if (stack->ga_len > 0) |
| 5235 | --stack->ga_len; |
| 5236 | } |
| 5237 | else if (lvar->lv_from_outer) |
| 5238 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
| 5239 | NULL); |
| 5240 | else |
| 5241 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5242 | } |
| 5243 | break; |
| 5244 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5245 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5246 | ret = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5247 | |
| 5248 | theend: |
| 5249 | vim_free(name); |
| 5250 | return ret; |
| 5251 | } |
| 5252 | |
| 5253 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5254 | * Check if "name" can be "unlet". |
| 5255 | */ |
| 5256 | int |
| 5257 | check_vim9_unlet(char_u *name) |
| 5258 | { |
| 5259 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5260 | { |
| 5261 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5262 | return FAIL; |
| 5263 | } |
| 5264 | return OK; |
| 5265 | } |
| 5266 | |
| 5267 | /* |
| 5268 | * Callback passed to ex_unletlock(). |
| 5269 | */ |
| 5270 | static int |
| 5271 | compile_unlet( |
| 5272 | lval_T *lvp, |
| 5273 | char_u *name_end, |
| 5274 | exarg_T *eap, |
| 5275 | int deep UNUSED, |
| 5276 | void *coookie) |
| 5277 | { |
| 5278 | cctx_T *cctx = coookie; |
| 5279 | |
| 5280 | if (lvp->ll_tv == NULL) |
| 5281 | { |
| 5282 | char_u *p = lvp->ll_name; |
| 5283 | int cc = *name_end; |
| 5284 | int ret = OK; |
| 5285 | |
| 5286 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5287 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5288 | if (*p == '$') |
| 5289 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5290 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5291 | ret = FAIL; |
| 5292 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5293 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5294 | |
| 5295 | *name_end = cc; |
| 5296 | return ret; |
| 5297 | } |
| 5298 | |
| 5299 | // TODO: unlet {list}[idx] |
| 5300 | // TODO: unlet {dict}[key] |
| 5301 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5302 | return FAIL; |
| 5303 | } |
| 5304 | |
| 5305 | /* |
| 5306 | * compile "unlet var", "lock var" and "unlock var" |
| 5307 | * "arg" points to "var". |
| 5308 | */ |
| 5309 | static char_u * |
| 5310 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5311 | { |
| 5312 | char_u *p = arg; |
| 5313 | |
| 5314 | if (eap->cmdidx != CMD_unlet) |
| 5315 | { |
| 5316 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5317 | return NULL; |
| 5318 | } |
| 5319 | |
| 5320 | if (*p == '!') |
| 5321 | { |
| 5322 | p = skipwhite(p + 1); |
| 5323 | eap->forceit = TRUE; |
| 5324 | } |
| 5325 | |
| 5326 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5327 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5328 | } |
| 5329 | |
| 5330 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5331 | * Compile an :import command. |
| 5332 | */ |
| 5333 | static char_u * |
| 5334 | compile_import(char_u *arg, cctx_T *cctx) |
| 5335 | { |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 5336 | return handle_import(arg, &cctx->ctx_imports, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | /* |
| 5340 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5341 | */ |
| 5342 | static int |
| 5343 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5344 | { |
| 5345 | garray_T *instr = &cctx->ctx_instr; |
| 5346 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5347 | |
| 5348 | if (endlabel == NULL) |
| 5349 | return FAIL; |
| 5350 | endlabel->el_next = *el; |
| 5351 | *el = endlabel; |
| 5352 | endlabel->el_end_label = instr->ga_len; |
| 5353 | |
| 5354 | generate_JUMP(cctx, when, 0); |
| 5355 | return OK; |
| 5356 | } |
| 5357 | |
| 5358 | static void |
| 5359 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5360 | { |
| 5361 | garray_T *instr = &cctx->ctx_instr; |
| 5362 | |
| 5363 | while (*el != NULL) |
| 5364 | { |
| 5365 | endlabel_T *cur = (*el); |
| 5366 | isn_T *isn; |
| 5367 | |
| 5368 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5369 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5370 | *el = cur->el_next; |
| 5371 | vim_free(cur); |
| 5372 | } |
| 5373 | } |
| 5374 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5375 | static void |
| 5376 | compile_free_jump_to_end(endlabel_T **el) |
| 5377 | { |
| 5378 | while (*el != NULL) |
| 5379 | { |
| 5380 | endlabel_T *cur = (*el); |
| 5381 | |
| 5382 | *el = cur->el_next; |
| 5383 | vim_free(cur); |
| 5384 | } |
| 5385 | } |
| 5386 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5387 | /* |
| 5388 | * Create a new scope and set up the generic items. |
| 5389 | */ |
| 5390 | static scope_T * |
| 5391 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5392 | { |
| 5393 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5394 | |
| 5395 | if (scope == NULL) |
| 5396 | return NULL; |
| 5397 | scope->se_outer = cctx->ctx_scope; |
| 5398 | cctx->ctx_scope = scope; |
| 5399 | scope->se_type = type; |
| 5400 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5401 | return scope; |
| 5402 | } |
| 5403 | |
| 5404 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5405 | * Free the current scope and go back to the outer scope. |
| 5406 | */ |
| 5407 | static void |
| 5408 | drop_scope(cctx_T *cctx) |
| 5409 | { |
| 5410 | scope_T *scope = cctx->ctx_scope; |
| 5411 | |
| 5412 | if (scope == NULL) |
| 5413 | { |
| 5414 | iemsg("calling drop_scope() without a scope"); |
| 5415 | return; |
| 5416 | } |
| 5417 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5418 | switch (scope->se_type) |
| 5419 | { |
| 5420 | case IF_SCOPE: |
| 5421 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5422 | case FOR_SCOPE: |
| 5423 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5424 | case WHILE_SCOPE: |
| 5425 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5426 | case TRY_SCOPE: |
| 5427 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5428 | case NO_SCOPE: |
| 5429 | case BLOCK_SCOPE: |
| 5430 | break; |
| 5431 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5432 | vim_free(scope); |
| 5433 | } |
| 5434 | |
| 5435 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5436 | * compile "if expr" |
| 5437 | * |
| 5438 | * "if expr" Produces instructions: |
| 5439 | * EVAL expr Push result of "expr" |
| 5440 | * JUMP_IF_FALSE end |
| 5441 | * ... body ... |
| 5442 | * end: |
| 5443 | * |
| 5444 | * "if expr | else" Produces instructions: |
| 5445 | * EVAL expr Push result of "expr" |
| 5446 | * JUMP_IF_FALSE else |
| 5447 | * ... body ... |
| 5448 | * JUMP_ALWAYS end |
| 5449 | * else: |
| 5450 | * ... body ... |
| 5451 | * end: |
| 5452 | * |
| 5453 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5454 | * EVAL expr Push result of "expr" |
| 5455 | * JUMP_IF_FALSE elseif |
| 5456 | * ... body ... |
| 5457 | * JUMP_ALWAYS end |
| 5458 | * elseif: |
| 5459 | * EVAL expr Push result of "expr" |
| 5460 | * JUMP_IF_FALSE else |
| 5461 | * ... body ... |
| 5462 | * JUMP_ALWAYS end |
| 5463 | * else: |
| 5464 | * ... body ... |
| 5465 | * end: |
| 5466 | */ |
| 5467 | static char_u * |
| 5468 | compile_if(char_u *arg, cctx_T *cctx) |
| 5469 | { |
| 5470 | char_u *p = arg; |
| 5471 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5472 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5473 | scope_T *scope; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5474 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5475 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5476 | CLEAR_FIELD(ppconst); |
| 5477 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5478 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5479 | clear_ppconst(&ppconst); |
| 5480 | return NULL; |
| 5481 | } |
| 5482 | if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
| 5483 | { |
| 5484 | // The expression results in a constant. |
| 5485 | // TODO: how about nesting? |
| 5486 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE; |
| 5487 | clear_ppconst(&ppconst); |
| 5488 | } |
| 5489 | else |
| 5490 | { |
| 5491 | // Not a constant, generate instructions for the expression. |
| 5492 | cctx->ctx_skip = MAYBE; |
| 5493 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5494 | return NULL; |
| 5495 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5496 | |
| 5497 | scope = new_scope(cctx, IF_SCOPE); |
| 5498 | if (scope == NULL) |
| 5499 | return NULL; |
| 5500 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5501 | if (cctx->ctx_skip == MAYBE) |
| 5502 | { |
| 5503 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5504 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5505 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5506 | } |
| 5507 | else |
| 5508 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5509 | |
| 5510 | return p; |
| 5511 | } |
| 5512 | |
| 5513 | static char_u * |
| 5514 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5515 | { |
| 5516 | char_u *p = arg; |
| 5517 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5518 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5519 | isn_T *isn; |
| 5520 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5521 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5522 | |
| 5523 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5524 | { |
| 5525 | emsg(_(e_elseif_without_if)); |
| 5526 | return NULL; |
| 5527 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5528 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5529 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5530 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5531 | { |
| 5532 | 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] | 5533 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5534 | return NULL; |
| 5535 | // previous "if" or "elseif" jumps here |
| 5536 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5537 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5538 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5539 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5540 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5541 | CLEAR_FIELD(ppconst); |
| 5542 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5543 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5544 | clear_ppconst(&ppconst); |
| 5545 | return NULL; |
| 5546 | } |
| 5547 | if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
| 5548 | { |
| 5549 | // The expression results in a constant. |
| 5550 | // TODO: how about nesting? |
| 5551 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? FALSE : TRUE; |
| 5552 | clear_ppconst(&ppconst); |
| 5553 | scope->se_u.se_if.is_if_label = -1; |
| 5554 | } |
| 5555 | else |
| 5556 | { |
| 5557 | // Not a constant, generate instructions for the expression. |
| 5558 | cctx->ctx_skip = MAYBE; |
| 5559 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5560 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5561 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5562 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5563 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5564 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5565 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5566 | |
| 5567 | return p; |
| 5568 | } |
| 5569 | |
| 5570 | static char_u * |
| 5571 | compile_else(char_u *arg, cctx_T *cctx) |
| 5572 | { |
| 5573 | char_u *p = arg; |
| 5574 | garray_T *instr = &cctx->ctx_instr; |
| 5575 | isn_T *isn; |
| 5576 | scope_T *scope = cctx->ctx_scope; |
| 5577 | |
| 5578 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5579 | { |
| 5580 | emsg(_(e_else_without_if)); |
| 5581 | return NULL; |
| 5582 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5583 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5584 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5585 | // jump from previous block to the end, unless the else block is empty |
| 5586 | if (cctx->ctx_skip == MAYBE) |
| 5587 | { |
| 5588 | 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] | 5589 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5590 | return NULL; |
| 5591 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5592 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5593 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5594 | { |
| 5595 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5596 | { |
| 5597 | // previous "if" or "elseif" jumps here |
| 5598 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5599 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5600 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | if (cctx->ctx_skip != MAYBE) |
| 5605 | cctx->ctx_skip = !cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5606 | |
| 5607 | return p; |
| 5608 | } |
| 5609 | |
| 5610 | static char_u * |
| 5611 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5612 | { |
| 5613 | scope_T *scope = cctx->ctx_scope; |
| 5614 | ifscope_T *ifscope; |
| 5615 | garray_T *instr = &cctx->ctx_instr; |
| 5616 | isn_T *isn; |
| 5617 | |
| 5618 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5619 | { |
| 5620 | emsg(_(e_endif_without_if)); |
| 5621 | return NULL; |
| 5622 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5623 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5624 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5625 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5626 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5627 | { |
| 5628 | // previous "if" or "elseif" jumps here |
| 5629 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5630 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5631 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5632 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5633 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5634 | cctx->ctx_skip = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5635 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5636 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5637 | return arg; |
| 5638 | } |
| 5639 | |
| 5640 | /* |
| 5641 | * compile "for var in expr" |
| 5642 | * |
| 5643 | * Produces instructions: |
| 5644 | * PUSHNR -1 |
| 5645 | * STORE loop-idx Set index to -1 |
| 5646 | * EVAL expr Push result of "expr" |
| 5647 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5648 | * - if beyond end, jump to "end" |
| 5649 | * - otherwise get item from list and push it |
| 5650 | * STORE var Store item in "var" |
| 5651 | * ... body ... |
| 5652 | * JUMP top Jump back to repeat |
| 5653 | * end: DROP Drop the result of "expr" |
| 5654 | * |
| 5655 | */ |
| 5656 | static char_u * |
| 5657 | compile_for(char_u *arg, cctx_T *cctx) |
| 5658 | { |
| 5659 | char_u *p; |
| 5660 | size_t varlen; |
| 5661 | garray_T *instr = &cctx->ctx_instr; |
| 5662 | garray_T *stack = &cctx->ctx_type_stack; |
| 5663 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5664 | lvar_T *loop_lvar; // loop iteration variable |
| 5665 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5666 | type_T *vartype; |
| 5667 | |
| 5668 | // TODO: list of variables: "for [key, value] in dict" |
| 5669 | // parse "var" |
| 5670 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5671 | ; |
| 5672 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5673 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5674 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5675 | { |
| 5676 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5677 | return NULL; |
| 5678 | } |
| 5679 | |
| 5680 | // consume "in" |
| 5681 | p = skipwhite(p); |
| 5682 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5683 | { |
| 5684 | emsg(_(e_missing_in)); |
| 5685 | return NULL; |
| 5686 | } |
| 5687 | p = skipwhite(p + 2); |
| 5688 | |
| 5689 | |
| 5690 | scope = new_scope(cctx, FOR_SCOPE); |
| 5691 | if (scope == NULL) |
| 5692 | return NULL; |
| 5693 | |
| 5694 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5695 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5696 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5697 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5698 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5699 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5700 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5701 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5702 | |
| 5703 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5704 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5705 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5706 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5707 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5708 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5709 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5710 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5711 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5712 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5713 | |
| 5714 | // compile "expr", it remains on the stack until "endfor" |
| 5715 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5716 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5717 | { |
| 5718 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5719 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5720 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5721 | |
| 5722 | // now we know the type of "var" |
| 5723 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5724 | if (vartype->tt_type != VAR_LIST) |
| 5725 | { |
| 5726 | emsg(_("E1024: need a List to iterate over")); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5727 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5728 | return NULL; |
| 5729 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5730 | if (vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5731 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5732 | |
| 5733 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5734 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5735 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5736 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5737 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5738 | |
| 5739 | return arg; |
| 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * compile "endfor" |
| 5744 | */ |
| 5745 | static char_u * |
| 5746 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5747 | { |
| 5748 | garray_T *instr = &cctx->ctx_instr; |
| 5749 | scope_T *scope = cctx->ctx_scope; |
| 5750 | forscope_T *forscope; |
| 5751 | isn_T *isn; |
| 5752 | |
| 5753 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5754 | { |
| 5755 | emsg(_(e_for)); |
| 5756 | return NULL; |
| 5757 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5758 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5759 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5760 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5761 | |
| 5762 | // At end of ":for" scope jump back to the FOR instruction. |
| 5763 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5764 | |
| 5765 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5766 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5767 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5768 | |
| 5769 | // Fill in the "end" label any BREAK statements |
| 5770 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5771 | |
| 5772 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5773 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5774 | return NULL; |
| 5775 | |
| 5776 | vim_free(scope); |
| 5777 | |
| 5778 | return arg; |
| 5779 | } |
| 5780 | |
| 5781 | /* |
| 5782 | * compile "while expr" |
| 5783 | * |
| 5784 | * Produces instructions: |
| 5785 | * top: EVAL expr Push result of "expr" |
| 5786 | * JUMP_IF_FALSE end jump if false |
| 5787 | * ... body ... |
| 5788 | * JUMP top Jump back to repeat |
| 5789 | * end: |
| 5790 | * |
| 5791 | */ |
| 5792 | static char_u * |
| 5793 | compile_while(char_u *arg, cctx_T *cctx) |
| 5794 | { |
| 5795 | char_u *p = arg; |
| 5796 | garray_T *instr = &cctx->ctx_instr; |
| 5797 | scope_T *scope; |
| 5798 | |
| 5799 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5800 | if (scope == NULL) |
| 5801 | return NULL; |
| 5802 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5803 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5804 | |
| 5805 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5806 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5807 | return NULL; |
| 5808 | |
| 5809 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5810 | 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] | 5811 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5812 | return FAIL; |
| 5813 | |
| 5814 | return p; |
| 5815 | } |
| 5816 | |
| 5817 | /* |
| 5818 | * compile "endwhile" |
| 5819 | */ |
| 5820 | static char_u * |
| 5821 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5822 | { |
| 5823 | scope_T *scope = cctx->ctx_scope; |
| 5824 | |
| 5825 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5826 | { |
| 5827 | emsg(_(e_while)); |
| 5828 | return NULL; |
| 5829 | } |
| 5830 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5831 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5832 | |
| 5833 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5834 | 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] | 5835 | |
| 5836 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5837 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5838 | 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] | 5839 | |
| 5840 | vim_free(scope); |
| 5841 | |
| 5842 | return arg; |
| 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | * compile "continue" |
| 5847 | */ |
| 5848 | static char_u * |
| 5849 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5850 | { |
| 5851 | scope_T *scope = cctx->ctx_scope; |
| 5852 | |
| 5853 | for (;;) |
| 5854 | { |
| 5855 | if (scope == NULL) |
| 5856 | { |
| 5857 | emsg(_(e_continue)); |
| 5858 | return NULL; |
| 5859 | } |
| 5860 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5861 | break; |
| 5862 | scope = scope->se_outer; |
| 5863 | } |
| 5864 | |
| 5865 | // Jump back to the FOR or WHILE instruction. |
| 5866 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5867 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5868 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5869 | return arg; |
| 5870 | } |
| 5871 | |
| 5872 | /* |
| 5873 | * compile "break" |
| 5874 | */ |
| 5875 | static char_u * |
| 5876 | compile_break(char_u *arg, cctx_T *cctx) |
| 5877 | { |
| 5878 | scope_T *scope = cctx->ctx_scope; |
| 5879 | endlabel_T **el; |
| 5880 | |
| 5881 | for (;;) |
| 5882 | { |
| 5883 | if (scope == NULL) |
| 5884 | { |
| 5885 | emsg(_(e_break)); |
| 5886 | return NULL; |
| 5887 | } |
| 5888 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5889 | break; |
| 5890 | scope = scope->se_outer; |
| 5891 | } |
| 5892 | |
| 5893 | // Jump to the end of the FOR or WHILE loop. |
| 5894 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5895 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5896 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5897 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5898 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5899 | return FAIL; |
| 5900 | |
| 5901 | return arg; |
| 5902 | } |
| 5903 | |
| 5904 | /* |
| 5905 | * compile "{" start of block |
| 5906 | */ |
| 5907 | static char_u * |
| 5908 | compile_block(char_u *arg, cctx_T *cctx) |
| 5909 | { |
| 5910 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5911 | return NULL; |
| 5912 | return skipwhite(arg + 1); |
| 5913 | } |
| 5914 | |
| 5915 | /* |
| 5916 | * compile end of block: drop one scope |
| 5917 | */ |
| 5918 | static void |
| 5919 | compile_endblock(cctx_T *cctx) |
| 5920 | { |
| 5921 | scope_T *scope = cctx->ctx_scope; |
| 5922 | |
| 5923 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5924 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5925 | vim_free(scope); |
| 5926 | } |
| 5927 | |
| 5928 | /* |
| 5929 | * compile "try" |
| 5930 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5931 | * finally. |
| 5932 | * Creates another scope for the "try" block itself. |
| 5933 | * TRY instruction sets up exception handling at runtime. |
| 5934 | * |
| 5935 | * "try" |
| 5936 | * TRY -> catch1, -> finally push trystack entry |
| 5937 | * ... try block |
| 5938 | * "throw {exception}" |
| 5939 | * EVAL {exception} |
| 5940 | * THROW create exception |
| 5941 | * ... try block |
| 5942 | * " catch {expr}" |
| 5943 | * JUMP -> finally |
| 5944 | * catch1: PUSH exeception |
| 5945 | * EVAL {expr} |
| 5946 | * MATCH |
| 5947 | * JUMP nomatch -> catch2 |
| 5948 | * CATCH remove exception |
| 5949 | * ... catch block |
| 5950 | * " catch" |
| 5951 | * JUMP -> finally |
| 5952 | * catch2: CATCH remove exception |
| 5953 | * ... catch block |
| 5954 | * " finally" |
| 5955 | * finally: |
| 5956 | * ... finally block |
| 5957 | * " endtry" |
| 5958 | * ENDTRY pop trystack entry, may rethrow |
| 5959 | */ |
| 5960 | static char_u * |
| 5961 | compile_try(char_u *arg, cctx_T *cctx) |
| 5962 | { |
| 5963 | garray_T *instr = &cctx->ctx_instr; |
| 5964 | scope_T *try_scope; |
| 5965 | scope_T *scope; |
| 5966 | |
| 5967 | // scope that holds the jumps that go to catch/finally/endtry |
| 5968 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5969 | if (try_scope == NULL) |
| 5970 | return NULL; |
| 5971 | |
| 5972 | // "catch" is set when the first ":catch" is found. |
| 5973 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5974 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5975 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5976 | return NULL; |
| 5977 | |
| 5978 | // scope for the try block itself |
| 5979 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5980 | if (scope == NULL) |
| 5981 | return NULL; |
| 5982 | |
| 5983 | return arg; |
| 5984 | } |
| 5985 | |
| 5986 | /* |
| 5987 | * compile "catch {expr}" |
| 5988 | */ |
| 5989 | static char_u * |
| 5990 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5991 | { |
| 5992 | scope_T *scope = cctx->ctx_scope; |
| 5993 | garray_T *instr = &cctx->ctx_instr; |
| 5994 | char_u *p; |
| 5995 | isn_T *isn; |
| 5996 | |
| 5997 | // end block scope from :try or :catch |
| 5998 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5999 | compile_endblock(cctx); |
| 6000 | scope = cctx->ctx_scope; |
| 6001 | |
| 6002 | // Error if not in a :try scope |
| 6003 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6004 | { |
| 6005 | emsg(_(e_catch)); |
| 6006 | return NULL; |
| 6007 | } |
| 6008 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6009 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6010 | { |
| 6011 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6012 | return NULL; |
| 6013 | } |
| 6014 | |
| 6015 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6016 | 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] | 6017 | JUMP_ALWAYS, cctx) == FAIL) |
| 6018 | return NULL; |
| 6019 | |
| 6020 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6021 | 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] | 6022 | if (isn->isn_arg.try.try_catch == 0) |
| 6023 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6024 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6025 | { |
| 6026 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6027 | 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] | 6028 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6029 | } |
| 6030 | |
| 6031 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6032 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6033 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6034 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6035 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6036 | } |
| 6037 | else |
| 6038 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6039 | char_u *end; |
| 6040 | char_u *pat; |
| 6041 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6042 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6043 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6044 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6045 | // Push v:exception, push {expr} and MATCH |
| 6046 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6047 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6048 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6049 | if (*end != *p) |
| 6050 | { |
| 6051 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6052 | vim_free(tofree); |
| 6053 | return FAIL; |
| 6054 | } |
| 6055 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6056 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6057 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6058 | len = (int)(end - tofree); |
| 6059 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6060 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6061 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6062 | if (pat == NULL) |
| 6063 | return FAIL; |
| 6064 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6065 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6066 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6067 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6068 | return NULL; |
| 6069 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6070 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6071 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6072 | return NULL; |
| 6073 | } |
| 6074 | |
| 6075 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6076 | return NULL; |
| 6077 | |
| 6078 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6079 | return NULL; |
| 6080 | return p; |
| 6081 | } |
| 6082 | |
| 6083 | static char_u * |
| 6084 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6085 | { |
| 6086 | scope_T *scope = cctx->ctx_scope; |
| 6087 | garray_T *instr = &cctx->ctx_instr; |
| 6088 | isn_T *isn; |
| 6089 | |
| 6090 | // end block scope from :try or :catch |
| 6091 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6092 | compile_endblock(cctx); |
| 6093 | scope = cctx->ctx_scope; |
| 6094 | |
| 6095 | // Error if not in a :try scope |
| 6096 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6097 | { |
| 6098 | emsg(_(e_finally)); |
| 6099 | return NULL; |
| 6100 | } |
| 6101 | |
| 6102 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6103 | 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] | 6104 | if (isn->isn_arg.try.try_finally != 0) |
| 6105 | { |
| 6106 | emsg(_(e_finally_dup)); |
| 6107 | return NULL; |
| 6108 | } |
| 6109 | |
| 6110 | // 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] | 6111 | 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] | 6112 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6113 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6114 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6115 | { |
| 6116 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6117 | 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] | 6118 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6119 | } |
| 6120 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6121 | // TODO: set index in ts_finally_label jumps |
| 6122 | |
| 6123 | return arg; |
| 6124 | } |
| 6125 | |
| 6126 | static char_u * |
| 6127 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6128 | { |
| 6129 | scope_T *scope = cctx->ctx_scope; |
| 6130 | garray_T *instr = &cctx->ctx_instr; |
| 6131 | isn_T *isn; |
| 6132 | |
| 6133 | // end block scope from :catch or :finally |
| 6134 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6135 | compile_endblock(cctx); |
| 6136 | scope = cctx->ctx_scope; |
| 6137 | |
| 6138 | // Error if not in a :try scope |
| 6139 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6140 | { |
| 6141 | if (scope == NULL) |
| 6142 | emsg(_(e_no_endtry)); |
| 6143 | else if (scope->se_type == WHILE_SCOPE) |
| 6144 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6145 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6146 | emsg(_(e_endfor)); |
| 6147 | else |
| 6148 | emsg(_(e_endif)); |
| 6149 | return NULL; |
| 6150 | } |
| 6151 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6152 | 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] | 6153 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6154 | { |
| 6155 | emsg(_("E1032: missing :catch or :finally")); |
| 6156 | return NULL; |
| 6157 | } |
| 6158 | |
| 6159 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6160 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6161 | 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] | 6162 | |
| 6163 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 6164 | if (isn->isn_arg.try.try_finally == 0) |
| 6165 | isn->isn_arg.try.try_finally = instr->ga_len; |
| 6166 | compile_endblock(cctx); |
| 6167 | |
| 6168 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6169 | return NULL; |
| 6170 | return arg; |
| 6171 | } |
| 6172 | |
| 6173 | /* |
| 6174 | * compile "throw {expr}" |
| 6175 | */ |
| 6176 | static char_u * |
| 6177 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6178 | { |
| 6179 | char_u *p = skipwhite(arg); |
| 6180 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6181 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6182 | return NULL; |
| 6183 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6184 | return NULL; |
| 6185 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6186 | return NULL; |
| 6187 | |
| 6188 | return p; |
| 6189 | } |
| 6190 | |
| 6191 | /* |
| 6192 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6193 | * compile "echomsg expr" |
| 6194 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6195 | * compile "execute expr" |
| 6196 | */ |
| 6197 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6198 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6199 | { |
| 6200 | char_u *p = arg; |
| 6201 | int count = 0; |
| 6202 | |
| 6203 | for (;;) |
| 6204 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6205 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6206 | return NULL; |
| 6207 | ++count; |
| 6208 | p = skipwhite(p); |
| 6209 | if (ends_excmd(*p)) |
| 6210 | break; |
| 6211 | } |
| 6212 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6213 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6214 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6215 | else if (cmdidx == CMD_execute) |
| 6216 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6217 | else if (cmdidx == CMD_echomsg) |
| 6218 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6219 | else |
| 6220 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6221 | return p; |
| 6222 | } |
| 6223 | |
| 6224 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6225 | * A command that is not compiled, execute with legacy code. |
| 6226 | */ |
| 6227 | static char_u * |
| 6228 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6229 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6230 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6231 | int has_expr = FALSE; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6232 | |
| 6233 | if (cctx->ctx_skip == TRUE) |
| 6234 | goto theend; |
| 6235 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6236 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
| 6237 | has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND)); |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6238 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6239 | { |
| 6240 | // expand filename in "syntax include [@group] filename" |
| 6241 | has_expr = TRUE; |
| 6242 | eap->arg = skipwhite(eap->arg + 7); |
| 6243 | if (*eap->arg == '@') |
| 6244 | eap->arg = skiptowhite(eap->arg); |
| 6245 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6246 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6247 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6248 | { |
| 6249 | int count = 0; |
| 6250 | char_u *start = skipwhite(line); |
| 6251 | |
| 6252 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6253 | // PUSHS ":cmd xxx" |
| 6254 | // eval expr1 |
| 6255 | // PUSHS "yyy" |
| 6256 | // eval expr2 |
| 6257 | // PUSHS "zzz" |
| 6258 | // EXECCONCAT 5 |
| 6259 | for (;;) |
| 6260 | { |
| 6261 | if (p > start) |
| 6262 | { |
| 6263 | generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start))); |
| 6264 | ++count; |
| 6265 | } |
| 6266 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6267 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6268 | return NULL; |
| 6269 | may_generate_2STRING(-1, cctx); |
| 6270 | ++count; |
| 6271 | p = skipwhite(p); |
| 6272 | if (*p != '`') |
| 6273 | { |
| 6274 | emsg(_("E1083: missing backtick")); |
| 6275 | return NULL; |
| 6276 | } |
| 6277 | start = p + 1; |
| 6278 | |
| 6279 | p = (char_u *)strstr((char *)start, "`="); |
| 6280 | if (p == NULL) |
| 6281 | { |
| 6282 | if (*skipwhite(start) != NUL) |
| 6283 | { |
| 6284 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6285 | ++count; |
| 6286 | } |
| 6287 | break; |
| 6288 | } |
| 6289 | } |
| 6290 | generate_EXECCONCAT(cctx, count); |
| 6291 | } |
| 6292 | else |
| 6293 | generate_EXEC(cctx, line); |
| 6294 | |
| 6295 | theend: |
| 6296 | return (char_u *)""; |
| 6297 | } |
| 6298 | |
| 6299 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6300 | * Add a function to the list of :def functions. |
| 6301 | * This "sets ufunc->uf_dfunc_idx" but the function isn't compiled yet. |
| 6302 | */ |
| 6303 | int |
| 6304 | add_def_function(ufunc_T *ufunc) |
| 6305 | { |
| 6306 | dfunc_T *dfunc; |
| 6307 | |
| 6308 | // Add the function to "def_functions". |
| 6309 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6310 | return FAIL; |
| 6311 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6312 | CLEAR_POINTER(dfunc); |
| 6313 | dfunc->df_idx = def_functions.ga_len; |
| 6314 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6315 | dfunc->df_ufunc = ufunc; |
| 6316 | ++def_functions.ga_len; |
| 6317 | return OK; |
| 6318 | } |
| 6319 | |
| 6320 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6321 | * After ex_function() has collected all the function lines: parse and compile |
| 6322 | * the lines into instructions. |
| 6323 | * Adds the function to "def_functions". |
| 6324 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6325 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6326 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6327 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6328 | * "def_functions". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6329 | */ |
| 6330 | void |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6331 | 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] | 6332 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6333 | char_u *line = NULL; |
| 6334 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6335 | char *errormsg = NULL; // error message |
| 6336 | int had_return = FALSE; |
| 6337 | cctx_T cctx; |
| 6338 | garray_T *instr; |
| 6339 | int called_emsg_before = called_emsg; |
| 6340 | int ret = FAIL; |
| 6341 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6342 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6343 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6344 | if (ufunc->uf_dfunc_idx >= 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6345 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6346 | // Redefining a function that was compiled before. |
| 6347 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6348 | + ufunc->uf_dfunc_idx; |
| 6349 | // Free old instructions. |
| 6350 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6351 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6352 | else if (add_def_function(ufunc) == FAIL) |
| 6353 | return; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6354 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6355 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6356 | cctx.ctx_ufunc = ufunc; |
| 6357 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6358 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6359 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6360 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6361 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6362 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6363 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6364 | instr = &cctx.ctx_instr; |
| 6365 | |
| 6366 | // Most modern script version. |
| 6367 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6368 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6369 | if (ufunc->uf_def_args.ga_len > 0) |
| 6370 | { |
| 6371 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6372 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6373 | int i; |
| 6374 | char_u *arg; |
| 6375 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6376 | |
| 6377 | // Produce instructions for the default values of optional arguments. |
| 6378 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6379 | // where to start when the function is called, depending on the number |
| 6380 | // of arguments. |
| 6381 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6382 | if (ufunc->uf_def_arg_idx == NULL) |
| 6383 | goto erret; |
| 6384 | for (i = 0; i < count; ++i) |
| 6385 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6386 | garray_T *stack = &cctx.ctx_type_stack; |
| 6387 | type_T *val_type; |
| 6388 | int arg_idx = first_def_arg + i; |
| 6389 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6390 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6391 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6392 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6393 | goto erret; |
| 6394 | |
| 6395 | // If no type specified use the type of the default value. |
| 6396 | // Otherwise check that the default value type matches the |
| 6397 | // specified type. |
| 6398 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6399 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6400 | ufunc->uf_arg_types[arg_idx] = val_type; |
| 6401 | else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE) |
| 6402 | == FAIL) |
| 6403 | { |
| 6404 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6405 | arg_idx + 1); |
| 6406 | goto erret; |
| 6407 | } |
| 6408 | |
| 6409 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6410 | goto erret; |
| 6411 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6412 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6413 | } |
| 6414 | |
| 6415 | /* |
| 6416 | * Loop over all the lines of the function and generate instructions. |
| 6417 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6418 | for (;;) |
| 6419 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6420 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6421 | int starts_with_colon = FALSE; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6422 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6423 | // Bail out on the first error to avoid a flood of errors and report |
| 6424 | // the right line number when inside try/catch. |
| 6425 | if (emsg_before != called_emsg) |
| 6426 | goto erret; |
| 6427 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6428 | if (line != NULL && *line == '|') |
| 6429 | // the line continues after a '|' |
| 6430 | ++line; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6431 | else if (line != NULL && *line != NUL |
| 6432 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6433 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6434 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6435 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6436 | goto erret; |
| 6437 | } |
| 6438 | else |
| 6439 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6440 | line = next_line_from_context(&cctx); |
| 6441 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6442 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6443 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6444 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6445 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6446 | |
| 6447 | had_return = FALSE; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6448 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6449 | ea.cmdlinep = &line; |
| 6450 | ea.cmd = skipwhite(line); |
| 6451 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6452 | // Some things can be recognized by the first character. |
| 6453 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6454 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6455 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6456 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6457 | if (ea.cmd[1] != '{') |
| 6458 | { |
| 6459 | line = (char_u *)""; |
| 6460 | continue; |
| 6461 | } |
| 6462 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6463 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6464 | case '}': |
| 6465 | { |
| 6466 | // "}" ends a block scope |
| 6467 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6468 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6469 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6470 | if (stype == BLOCK_SCOPE) |
| 6471 | { |
| 6472 | compile_endblock(&cctx); |
| 6473 | line = ea.cmd; |
| 6474 | } |
| 6475 | else |
| 6476 | { |
| 6477 | emsg(_("E1025: using } outside of a block scope")); |
| 6478 | goto erret; |
| 6479 | } |
| 6480 | if (line != NULL) |
| 6481 | line = skipwhite(ea.cmd + 1); |
| 6482 | continue; |
| 6483 | } |
| 6484 | |
| 6485 | case '{': |
| 6486 | // "{" starts a block scope |
| 6487 | // "{'a': 1}->func() is something else |
| 6488 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6489 | { |
| 6490 | line = compile_block(ea.cmd, &cctx); |
| 6491 | continue; |
| 6492 | } |
| 6493 | break; |
| 6494 | |
| 6495 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6496 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6497 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6498 | } |
| 6499 | |
| 6500 | /* |
| 6501 | * COMMAND MODIFIERS |
| 6502 | */ |
| 6503 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6504 | { |
| 6505 | if (errormsg != NULL) |
| 6506 | goto erret; |
| 6507 | // empty line or comment |
| 6508 | line = (char_u *)""; |
| 6509 | continue; |
| 6510 | } |
| 6511 | |
| 6512 | // Skip ":call" to get to the function name. |
| 6513 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 6514 | ea.cmd = skipwhite(ea.cmd); |
| 6515 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6516 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6517 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6518 | char_u *pskip; |
| 6519 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6520 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6521 | // find what follows. |
| 6522 | // Skip over "var.member", "var[idx]" and the like. |
| 6523 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6524 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6525 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6526 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6527 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6528 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6529 | char_u *var_end; |
| 6530 | int oplen; |
| 6531 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6532 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6533 | var_end = find_name_end(pskip, NULL, NULL, |
| 6534 | FNE_CHECK_START | FNE_INCL_BR); |
| 6535 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6536 | if (oplen > 0) |
| 6537 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6538 | size_t len = p - ea.cmd; |
| 6539 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6540 | // Recognize an assignment if we recognize the variable |
| 6541 | // name: |
| 6542 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6543 | // "local = expr" where "local" is a local var. |
| 6544 | // "script = expr" where "script" is a script-local var. |
| 6545 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6546 | // "&opt = expr" |
| 6547 | // "$ENV = expr" |
| 6548 | // "@r = expr" |
| 6549 | if (*ea.cmd == '&' |
| 6550 | || *ea.cmd == '$' |
| 6551 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6552 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6553 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6554 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6555 | NULL, &cctx) == OK |
| 6556 | || lookup_script(ea.cmd, len) == OK |
| 6557 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6558 | { |
| 6559 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6560 | if (line == NULL) |
| 6561 | goto erret; |
| 6562 | continue; |
| 6563 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6564 | } |
| 6565 | } |
| 6566 | } |
| 6567 | |
| 6568 | /* |
| 6569 | * COMMAND after range |
| 6570 | */ |
| 6571 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6572 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6573 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6574 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6575 | |
| 6576 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6577 | { |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6578 | if (cctx.ctx_skip == TRUE) |
| 6579 | { |
| 6580 | line += STRLEN(line); |
| 6581 | continue; |
| 6582 | } |
| 6583 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6584 | // Expression or function call. |
| 6585 | if (ea.cmdidx == CMD_eval) |
| 6586 | { |
| 6587 | p = ea.cmd; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6588 | if (compile_expr0(&p, &cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6589 | goto erret; |
| 6590 | |
| 6591 | // drop the return value |
| 6592 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6593 | line = p; |
| 6594 | continue; |
| 6595 | } |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6596 | // CMD_let cannot happen, compile_assignment() above is used |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6597 | iemsg("Command from find_ex_command() not handled"); |
| 6598 | goto erret; |
| 6599 | } |
| 6600 | |
| 6601 | p = skipwhite(p); |
| 6602 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6603 | if (cctx.ctx_skip == TRUE |
| 6604 | && ea.cmdidx != CMD_elseif |
| 6605 | && ea.cmdidx != CMD_else |
| 6606 | && ea.cmdidx != CMD_endif) |
| 6607 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6608 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6609 | continue; |
| 6610 | } |
| 6611 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6612 | switch (ea.cmdidx) |
| 6613 | { |
| 6614 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6615 | ea.arg = p; |
| 6616 | line = compile_nested_function(&ea, &cctx); |
| 6617 | break; |
| 6618 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6619 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6620 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6621 | goto erret; |
| 6622 | |
| 6623 | case CMD_return: |
| 6624 | line = compile_return(p, set_return_type, &cctx); |
| 6625 | had_return = TRUE; |
| 6626 | break; |
| 6627 | |
| 6628 | case CMD_let: |
| 6629 | case CMD_const: |
| 6630 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
| 6631 | break; |
| 6632 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6633 | case CMD_unlet: |
| 6634 | case CMD_unlockvar: |
| 6635 | case CMD_lockvar: |
| 6636 | line = compile_unletlock(p, &ea, &cctx); |
| 6637 | break; |
| 6638 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6639 | case CMD_import: |
| 6640 | line = compile_import(p, &cctx); |
| 6641 | break; |
| 6642 | |
| 6643 | case CMD_if: |
| 6644 | line = compile_if(p, &cctx); |
| 6645 | break; |
| 6646 | case CMD_elseif: |
| 6647 | line = compile_elseif(p, &cctx); |
| 6648 | break; |
| 6649 | case CMD_else: |
| 6650 | line = compile_else(p, &cctx); |
| 6651 | break; |
| 6652 | case CMD_endif: |
| 6653 | line = compile_endif(p, &cctx); |
| 6654 | break; |
| 6655 | |
| 6656 | case CMD_while: |
| 6657 | line = compile_while(p, &cctx); |
| 6658 | break; |
| 6659 | case CMD_endwhile: |
| 6660 | line = compile_endwhile(p, &cctx); |
| 6661 | break; |
| 6662 | |
| 6663 | case CMD_for: |
| 6664 | line = compile_for(p, &cctx); |
| 6665 | break; |
| 6666 | case CMD_endfor: |
| 6667 | line = compile_endfor(p, &cctx); |
| 6668 | break; |
| 6669 | case CMD_continue: |
| 6670 | line = compile_continue(p, &cctx); |
| 6671 | break; |
| 6672 | case CMD_break: |
| 6673 | line = compile_break(p, &cctx); |
| 6674 | break; |
| 6675 | |
| 6676 | case CMD_try: |
| 6677 | line = compile_try(p, &cctx); |
| 6678 | break; |
| 6679 | case CMD_catch: |
| 6680 | line = compile_catch(p, &cctx); |
| 6681 | break; |
| 6682 | case CMD_finally: |
| 6683 | line = compile_finally(p, &cctx); |
| 6684 | break; |
| 6685 | case CMD_endtry: |
| 6686 | line = compile_endtry(p, &cctx); |
| 6687 | break; |
| 6688 | case CMD_throw: |
| 6689 | line = compile_throw(p, &cctx); |
| 6690 | break; |
| 6691 | |
| 6692 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6693 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6694 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6695 | case CMD_echomsg: |
| 6696 | case CMD_echoerr: |
| 6697 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6698 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6699 | |
| 6700 | default: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6701 | // TODO: other commands with an expression argument |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6702 | // Not recognized, execute with do_cmdline_cmd(). |
| 6703 | ea.arg = p; |
| 6704 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6705 | break; |
| 6706 | } |
| 6707 | if (line == NULL) |
| 6708 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6709 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6710 | |
| 6711 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6712 | { |
| 6713 | iemsg("Type stack underflow"); |
| 6714 | goto erret; |
| 6715 | } |
| 6716 | } |
| 6717 | |
| 6718 | if (cctx.ctx_scope != NULL) |
| 6719 | { |
| 6720 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6721 | emsg(_(e_endif)); |
| 6722 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6723 | emsg(_(e_endwhile)); |
| 6724 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6725 | emsg(_(e_endfor)); |
| 6726 | else |
| 6727 | emsg(_("E1026: Missing }")); |
| 6728 | goto erret; |
| 6729 | } |
| 6730 | |
| 6731 | if (!had_return) |
| 6732 | { |
| 6733 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6734 | { |
| 6735 | emsg(_("E1027: Missing return statement")); |
| 6736 | goto erret; |
| 6737 | } |
| 6738 | |
| 6739 | // Return zero if there is no return at the end. |
| 6740 | generate_PUSHNR(&cctx, 0); |
| 6741 | generate_instr(&cctx, ISN_RETURN); |
| 6742 | } |
| 6743 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6744 | { |
| 6745 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6746 | + ufunc->uf_dfunc_idx; |
| 6747 | dfunc->df_deleted = FALSE; |
| 6748 | dfunc->df_instr = instr->ga_data; |
| 6749 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6750 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6751 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6752 | if (cctx.ctx_outer_used) |
| 6753 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6754 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6755 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6756 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6757 | int varargs = ufunc->uf_va_name != NULL; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6758 | int argcount = ufunc->uf_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6759 | |
| 6760 | // Create a type for the function, with the return type and any |
| 6761 | // argument types. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6762 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6763 | // The type is included in "tt_args". |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6764 | if (argcount > 0 || varargs) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6765 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6766 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6767 | argcount, &ufunc->uf_type_list); |
| 6768 | // Add argument types to the function type. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6769 | if (func_type_add_arg_types(ufunc->uf_func_type, |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6770 | argcount + varargs, |
| 6771 | &ufunc->uf_type_list) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6772 | { |
| 6773 | ret = FAIL; |
| 6774 | goto erret; |
| 6775 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6776 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6777 | ufunc->uf_func_type->tt_min_argcount = |
| 6778 | argcount - ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6779 | if (ufunc->uf_arg_types == NULL) |
| 6780 | { |
| 6781 | int i; |
| 6782 | |
| 6783 | // lambda does not have argument types. |
| 6784 | for (i = 0; i < argcount; ++i) |
| 6785 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6786 | } |
| 6787 | else |
| 6788 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6789 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6790 | if (varargs) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6791 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6792 | ufunc->uf_func_type->tt_args[argcount] = |
| 6793 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6794 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6795 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6796 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6797 | else |
| 6798 | // No arguments, can use a predefined type. |
| 6799 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6800 | argcount, &ufunc->uf_type_list); |
| 6801 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6802 | } |
| 6803 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6804 | ret = OK; |
| 6805 | |
| 6806 | erret: |
| 6807 | if (ret == FAIL) |
| 6808 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6809 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6810 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6811 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6812 | |
| 6813 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6814 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6815 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6816 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6817 | ufunc->uf_dfunc_idx = -1; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6818 | if (!dfunc->df_deleted) |
| 6819 | --def_functions.ga_len; |
| 6820 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6821 | while (cctx.ctx_scope != NULL) |
| 6822 | drop_scope(&cctx); |
| 6823 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6824 | // Don't execute this function body. |
| 6825 | ga_clear_strings(&ufunc->uf_lines); |
| 6826 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6827 | if (errormsg != NULL) |
| 6828 | emsg(errormsg); |
| 6829 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6830 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | current_sctx = save_current_sctx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6834 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6835 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6836 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | /* |
| 6840 | * Delete an instruction, free what it contains. |
| 6841 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6842 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6843 | delete_instr(isn_T *isn) |
| 6844 | { |
| 6845 | switch (isn->isn_type) |
| 6846 | { |
| 6847 | case ISN_EXEC: |
| 6848 | case ISN_LOADENV: |
| 6849 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6850 | case ISN_LOADB: |
| 6851 | case ISN_LOADW: |
| 6852 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6853 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6854 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6855 | case ISN_PUSHEXC: |
| 6856 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6857 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6858 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6859 | case ISN_STOREB: |
| 6860 | case ISN_STOREW: |
| 6861 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6862 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6863 | vim_free(isn->isn_arg.string); |
| 6864 | break; |
| 6865 | |
| 6866 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6867 | case ISN_STORES: |
| 6868 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6869 | break; |
| 6870 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6871 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 6872 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6873 | vim_free(isn->isn_arg.unlet.ul_name); |
| 6874 | break; |
| 6875 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6876 | case ISN_STOREOPT: |
| 6877 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6878 | break; |
| 6879 | |
| 6880 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6881 | blob_unref(isn->isn_arg.blob); |
| 6882 | break; |
| 6883 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6884 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6885 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6886 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6887 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6888 | break; |
| 6889 | |
| 6890 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6891 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6892 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6893 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6894 | break; |
| 6895 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6896 | case ISN_UCALL: |
| 6897 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6898 | break; |
| 6899 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 6900 | case ISN_FUNCREF: |
| 6901 | { |
| 6902 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6903 | + isn->isn_arg.funcref.fr_func; |
| 6904 | func_ptr_unref(dfunc->df_ufunc); |
| 6905 | } |
| 6906 | break; |
| 6907 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6908 | case ISN_2BOOL: |
| 6909 | case ISN_2STRING: |
| 6910 | case ISN_ADDBLOB: |
| 6911 | case ISN_ADDLIST: |
| 6912 | case ISN_BCALL: |
| 6913 | case ISN_CATCH: |
| 6914 | case ISN_CHECKNR: |
| 6915 | case ISN_CHECKTYPE: |
| 6916 | case ISN_COMPAREANY: |
| 6917 | case ISN_COMPAREBLOB: |
| 6918 | case ISN_COMPAREBOOL: |
| 6919 | case ISN_COMPAREDICT: |
| 6920 | case ISN_COMPAREFLOAT: |
| 6921 | case ISN_COMPAREFUNC: |
| 6922 | case ISN_COMPARELIST: |
| 6923 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6924 | case ISN_COMPARESPECIAL: |
| 6925 | case ISN_COMPARESTRING: |
| 6926 | case ISN_CONCAT: |
| 6927 | case ISN_DCALL: |
| 6928 | case ISN_DROP: |
| 6929 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6930 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6931 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6932 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6933 | case ISN_EXECCONCAT: |
| 6934 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6935 | case ISN_FOR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6936 | case ISN_INDEX: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6937 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6938 | case ISN_JUMP: |
| 6939 | case ISN_LOAD: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6940 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6941 | case ISN_LOADSCRIPT: |
| 6942 | case ISN_LOADREG: |
| 6943 | case ISN_LOADV: |
| 6944 | case ISN_NEGATENR: |
| 6945 | case ISN_NEWDICT: |
| 6946 | case ISN_NEWLIST: |
| 6947 | case ISN_OPNR: |
| 6948 | case ISN_OPFLOAT: |
| 6949 | case ISN_OPANY: |
| 6950 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6951 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6952 | case ISN_PUSHF: |
| 6953 | case ISN_PUSHNR: |
| 6954 | case ISN_PUSHBOOL: |
| 6955 | case ISN_PUSHSPEC: |
| 6956 | case ISN_RETURN: |
| 6957 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 6958 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6959 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6960 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6961 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6962 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6963 | case ISN_STOREDICT: |
| 6964 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6965 | case ISN_THROW: |
| 6966 | case ISN_TRY: |
| 6967 | // nothing allocated |
| 6968 | break; |
| 6969 | } |
| 6970 | } |
| 6971 | |
| 6972 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6973 | * Free all instructions for "dfunc". |
| 6974 | */ |
| 6975 | static void |
| 6976 | delete_def_function_contents(dfunc_T *dfunc) |
| 6977 | { |
| 6978 | int idx; |
| 6979 | |
| 6980 | ga_clear(&dfunc->df_def_args_isn); |
| 6981 | |
| 6982 | if (dfunc->df_instr != NULL) |
| 6983 | { |
| 6984 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 6985 | delete_instr(dfunc->df_instr + idx); |
| 6986 | VIM_CLEAR(dfunc->df_instr); |
| 6987 | } |
| 6988 | |
| 6989 | dfunc->df_deleted = TRUE; |
| 6990 | } |
| 6991 | |
| 6992 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6993 | * When a user function is deleted, delete any associated def function. |
| 6994 | */ |
| 6995 | void |
| 6996 | delete_def_function(ufunc_T *ufunc) |
| 6997 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6998 | if (ufunc->uf_dfunc_idx >= 0) |
| 6999 | { |
| 7000 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7001 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7002 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7003 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7004 | } |
| 7005 | } |
| 7006 | |
| 7007 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7008 | /* |
| 7009 | * Free all functions defined with ":def". |
| 7010 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7011 | void |
| 7012 | free_def_functions(void) |
| 7013 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7014 | int idx; |
| 7015 | |
| 7016 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7017 | { |
| 7018 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7019 | |
| 7020 | delete_def_function_contents(dfunc); |
| 7021 | } |
| 7022 | |
| 7023 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7024 | } |
| 7025 | #endif |
| 7026 | |
| 7027 | |
| 7028 | #endif // FEAT_EVAL |