Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
| 26 | /* |
| 27 | * Chain of jump instructions where the end label needs to be set. |
| 28 | */ |
| 29 | typedef struct endlabel_S endlabel_T; |
| 30 | struct endlabel_S { |
| 31 | endlabel_T *el_next; // chain end_label locations |
| 32 | int el_end_label; // instruction idx where to set end |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * info specific for the scope of :if / elseif / else |
| 37 | */ |
| 38 | typedef struct { |
| 39 | int is_if_label; // instruction idx at IF or ELSEIF |
| 40 | endlabel_T *is_end_label; // instructions to set end label |
| 41 | } ifscope_T; |
| 42 | |
| 43 | /* |
| 44 | * info specific for the scope of :while |
| 45 | */ |
| 46 | typedef struct { |
| 47 | int ws_top_label; // instruction idx at WHILE |
| 48 | endlabel_T *ws_end_label; // instructions to set end |
| 49 | } whilescope_T; |
| 50 | |
| 51 | /* |
| 52 | * info specific for the scope of :for |
| 53 | */ |
| 54 | typedef struct { |
| 55 | int fs_top_label; // instruction idx at FOR |
| 56 | endlabel_T *fs_end_label; // break instructions |
| 57 | } forscope_T; |
| 58 | |
| 59 | /* |
| 60 | * info specific for the scope of :try |
| 61 | */ |
| 62 | typedef struct { |
| 63 | int ts_try_label; // instruction idx at TRY |
| 64 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 65 | int ts_catch_label; // instruction idx of last CATCH |
| 66 | int ts_caught_all; // "catch" without argument encountered |
| 67 | } tryscope_T; |
| 68 | |
| 69 | typedef enum { |
| 70 | NO_SCOPE, |
| 71 | IF_SCOPE, |
| 72 | WHILE_SCOPE, |
| 73 | FOR_SCOPE, |
| 74 | TRY_SCOPE, |
| 75 | BLOCK_SCOPE |
| 76 | } scopetype_T; |
| 77 | |
| 78 | /* |
| 79 | * Info for one scope, pointed to by "ctx_scope". |
| 80 | */ |
| 81 | typedef struct scope_S scope_T; |
| 82 | struct scope_S { |
| 83 | scope_T *se_outer; // scope containing this one |
| 84 | scopetype_T se_type; |
| 85 | int se_local_count; // ctx_locals.ga_len before scope |
| 86 | union { |
| 87 | ifscope_T se_if; |
| 88 | whilescope_T se_while; |
| 89 | forscope_T se_for; |
| 90 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 91 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 96 | */ |
| 97 | typedef struct { |
| 98 | char_u *lv_name; |
| 99 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 100 | int lv_idx; // index of the variable on the stack |
| 101 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 102 | int lv_const; // when TRUE cannot be assigned to |
| 103 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 104 | } lvar_T; |
| 105 | |
| 106 | /* |
| 107 | * Context for compiling lines of Vim script. |
| 108 | * Stores info about the local variables and condition stack. |
| 109 | */ |
| 110 | struct cctx_S { |
| 111 | ufunc_T *ctx_ufunc; // current function |
| 112 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 113 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | garray_T ctx_instr; // generated instructions |
| 115 | |
| 116 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 117 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 118 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 119 | int ctx_closure_count; // number of closures created in the |
| 120 | // function |
| 121 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 122 | garray_T ctx_imports; // imported items |
| 123 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 124 | int ctx_skip; // when TRUE skip commands, when FALSE skip |
| 125 | // commands after "else" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 126 | scope_T *ctx_scope; // current scope, NULL at toplevel |
| 127 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 128 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 129 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 130 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 133 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 137 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
| 138 | |
| 139 | static int compile_expr1(char_u **arg, cctx_T *cctx); |
| 140 | static int compile_expr2(char_u **arg, cctx_T *cctx); |
| 141 | static int compile_expr3(char_u **arg, cctx_T *cctx); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 142 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 143 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
| 144 | static int check_type(type_T *expected, type_T *actual, int give_msg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 145 | |
| 146 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 147 | * Lookup variable "name" in the local scope and return it. |
| 148 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 149 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 150 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 152 | { |
| 153 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 154 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 155 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 156 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 157 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 158 | |
| 159 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 161 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 162 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 163 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 164 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 165 | { |
| 166 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 167 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 168 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | |
| 171 | // Find local in outer function scope. |
| 172 | if (cctx->ctx_outer != NULL) |
| 173 | { |
| 174 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 175 | if (lvar != NULL) |
| 176 | { |
| 177 | // TODO: are there situations we should not mark the outer scope as |
| 178 | // used? |
| 179 | cctx->ctx_outer_used = TRUE; |
| 180 | lvar->lv_from_outer = TRUE; |
| 181 | return lvar; |
| 182 | } |
| 183 | } |
| 184 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 185 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 189 | * Lookup an argument in the current function and an enclosing function. |
| 190 | * Returns the argument index in "idxp" |
| 191 | * Returns the argument type in "type" |
| 192 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 193 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 194 | */ |
| 195 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 196 | lookup_arg( |
| 197 | char_u *name, |
| 198 | size_t len, |
| 199 | int *idxp, |
| 200 | type_T **type, |
| 201 | int *gen_load_outer, |
| 202 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | { |
| 204 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 206 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 207 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 208 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 209 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 210 | { |
| 211 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 212 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 213 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 214 | { |
| 215 | if (idxp != NULL) |
| 216 | { |
| 217 | // Arguments are located above the frame pointer. One further |
| 218 | // if there is a vararg argument |
| 219 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 220 | + STACK_FRAME_SIZE) |
| 221 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 222 | |
| 223 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 224 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 225 | else |
| 226 | *type = &t_any; |
| 227 | } |
| 228 | return OK; |
| 229 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 230 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 231 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 232 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 233 | if (va_name != NULL |
| 234 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 235 | { |
| 236 | if (idxp != NULL) |
| 237 | { |
| 238 | // varargs is always the last argument |
| 239 | *idxp = -STACK_FRAME_SIZE - 1; |
| 240 | *type = cctx->ctx_ufunc->uf_va_type; |
| 241 | } |
| 242 | return OK; |
| 243 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 244 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 245 | if (cctx->ctx_outer != NULL) |
| 246 | { |
| 247 | // Lookup the name for an argument of the outer function. |
| 248 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 249 | == OK) |
| 250 | { |
| 251 | *gen_load_outer = TRUE; |
| 252 | return OK; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Lookup a variable in the current script. |
| 261 | * Returns OK or FAIL. |
| 262 | */ |
| 263 | static int |
| 264 | lookup_script(char_u *name, size_t len) |
| 265 | { |
| 266 | int cc; |
| 267 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 268 | dictitem_T *di; |
| 269 | |
| 270 | cc = name[len]; |
| 271 | name[len] = NUL; |
| 272 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 273 | name[len] = cc; |
| 274 | return di == NULL ? FAIL: OK; |
| 275 | } |
| 276 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 279 | * compilation context "cctx". |
| 280 | * Return FAIL and give an error if it defined. |
| 281 | */ |
| 282 | int |
| 283 | check_defined(char_u *p, int len, cctx_T *cctx) |
| 284 | { |
| 285 | if (lookup_script(p, len) == OK |
| 286 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 287 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 288 | || find_imported(p, len, cctx) != NULL))) |
| 289 | { |
| 290 | semsg("E1073: imported name already defined: %s", p); |
| 291 | return FAIL; |
| 292 | } |
| 293 | return OK; |
| 294 | } |
| 295 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 296 | /* |
| 297 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 298 | * be freed later. |
| 299 | */ |
| 300 | static type_T * |
| 301 | alloc_type(garray_T *type_gap) |
| 302 | { |
| 303 | type_T *type; |
| 304 | |
| 305 | if (ga_grow(type_gap, 1) == FAIL) |
| 306 | return NULL; |
| 307 | type = ALLOC_CLEAR_ONE(type_T); |
| 308 | if (type != NULL) |
| 309 | { |
| 310 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 311 | ++type_gap->ga_len; |
| 312 | } |
| 313 | return type; |
| 314 | } |
| 315 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 316 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 317 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 318 | { |
| 319 | type_T *type; |
| 320 | |
| 321 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 322 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 323 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 324 | if (member_type->tt_type == VAR_VOID |
| 325 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 326 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 327 | if (member_type->tt_type == VAR_BOOL) |
| 328 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 329 | if (member_type->tt_type == VAR_NUMBER) |
| 330 | return &t_list_number; |
| 331 | if (member_type->tt_type == VAR_STRING) |
| 332 | return &t_list_string; |
| 333 | |
| 334 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 335 | type = alloc_type(type_gap); |
| 336 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 337 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 338 | type->tt_type = VAR_LIST; |
| 339 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 340 | type->tt_argcount = 0; |
| 341 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 342 | return type; |
| 343 | } |
| 344 | |
| 345 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 346 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 347 | { |
| 348 | type_T *type; |
| 349 | |
| 350 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 351 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 352 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 353 | if (member_type->tt_type == VAR_VOID |
| 354 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 355 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 356 | if (member_type->tt_type == VAR_BOOL) |
| 357 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 358 | if (member_type->tt_type == VAR_NUMBER) |
| 359 | return &t_dict_number; |
| 360 | if (member_type->tt_type == VAR_STRING) |
| 361 | return &t_dict_string; |
| 362 | |
| 363 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 364 | type = alloc_type(type_gap); |
| 365 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 366 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 367 | type->tt_type = VAR_DICT; |
| 368 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 369 | type->tt_argcount = 0; |
| 370 | type->tt_args = NULL; |
| 371 | return type; |
| 372 | } |
| 373 | |
| 374 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 375 | * Allocate a new type for a function. |
| 376 | */ |
| 377 | static type_T * |
| 378 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 379 | { |
| 380 | type_T *type = alloc_type(type_gap); |
| 381 | |
| 382 | if (type == NULL) |
| 383 | return &t_any; |
| 384 | type->tt_type = VAR_FUNC; |
| 385 | type->tt_member = ret_type; |
| 386 | type->tt_argcount = argcount; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 392 | * Get a function type, based on the return type "ret_type". |
| 393 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 394 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 395 | */ |
| 396 | static type_T * |
| 397 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 398 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 399 | // recognize commonly used types |
| 400 | if (argcount <= 0) |
| 401 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 402 | if (ret_type == &t_unknown) |
| 403 | { |
| 404 | // (argcount == 0) is not possible |
| 405 | return &t_func_unknown; |
| 406 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 407 | if (ret_type == &t_void) |
| 408 | { |
| 409 | if (argcount == 0) |
| 410 | return &t_func_0_void; |
| 411 | else |
| 412 | return &t_func_void; |
| 413 | } |
| 414 | if (ret_type == &t_any) |
| 415 | { |
| 416 | if (argcount == 0) |
| 417 | return &t_func_0_any; |
| 418 | else |
| 419 | return &t_func_any; |
| 420 | } |
| 421 | if (ret_type == &t_number) |
| 422 | { |
| 423 | if (argcount == 0) |
| 424 | return &t_func_0_number; |
| 425 | else |
| 426 | return &t_func_number; |
| 427 | } |
| 428 | if (ret_type == &t_string) |
| 429 | { |
| 430 | if (argcount == 0) |
| 431 | return &t_func_0_string; |
| 432 | else |
| 433 | return &t_func_string; |
| 434 | } |
| 435 | } |
| 436 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 437 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 440 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 441 | * For a function type, reserve space for "argcount" argument types (including |
| 442 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 443 | */ |
| 444 | static int |
| 445 | func_type_add_arg_types( |
| 446 | type_T *functype, |
| 447 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 448 | garray_T *type_gap) |
| 449 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 450 | // To make it easy to free the space needed for the argument types, add the |
| 451 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 452 | if (ga_grow(type_gap, 1) == FAIL) |
| 453 | return FAIL; |
| 454 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 455 | if (functype->tt_args == NULL) |
| 456 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 457 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 458 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 459 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | return OK; |
| 461 | } |
| 462 | |
| 463 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 464 | * Return the type_T for a typval. Only for primitive types. |
| 465 | */ |
| 466 | static type_T * |
| 467 | typval2type(typval_T *tv) |
| 468 | { |
| 469 | if (tv->v_type == VAR_NUMBER) |
| 470 | return &t_number; |
| 471 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 472 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 473 | if (tv->v_type == VAR_STRING) |
| 474 | return &t_string; |
| 475 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 476 | return &t_list_string; |
| 477 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 478 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 479 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 482 | static void |
| 483 | type_mismatch(type_T *expected, type_T *actual) |
| 484 | { |
| 485 | char *tofree1, *tofree2; |
| 486 | |
| 487 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 488 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 489 | vim_free(tofree1); |
| 490 | vim_free(tofree2); |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 495 | { |
| 496 | char *tofree1, *tofree2; |
| 497 | |
| 498 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 499 | argidx, |
| 500 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 501 | vim_free(tofree1); |
| 502 | vim_free(tofree2); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Check if the expected and actual types match. |
| 507 | * Does not allow for assigning "any" to a specific type. |
| 508 | */ |
| 509 | static int |
| 510 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 511 | { |
| 512 | int ret = OK; |
| 513 | |
| 514 | // When expected is "unknown" we accept any actual type. |
| 515 | // When expected is "any" we accept any actual type except "void". |
| 516 | if (expected->tt_type != VAR_UNKNOWN |
| 517 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 518 | |
| 519 | { |
| 520 | if (expected->tt_type != actual->tt_type) |
| 521 | { |
| 522 | if (give_msg) |
| 523 | type_mismatch(expected, actual); |
| 524 | return FAIL; |
| 525 | } |
| 526 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 527 | { |
| 528 | // "unknown" is used for an empty list or dict |
| 529 | if (actual->tt_member != &t_unknown) |
| 530 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 531 | } |
| 532 | else if (expected->tt_type == VAR_FUNC) |
| 533 | { |
| 534 | if (expected->tt_member != &t_unknown) |
| 535 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 536 | if (ret == OK && expected->tt_argcount != -1 |
| 537 | && (actual->tt_argcount < expected->tt_min_argcount |
| 538 | || actual->tt_argcount > expected->tt_argcount)) |
| 539 | ret = FAIL; |
| 540 | } |
| 541 | if (ret == FAIL && give_msg) |
| 542 | type_mismatch(expected, actual); |
| 543 | } |
| 544 | return ret; |
| 545 | } |
| 546 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 547 | ///////////////////////////////////////////////////////////////////// |
| 548 | // Following generate_ functions expect the caller to call ga_grow(). |
| 549 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 550 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL |
| 551 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK |
| 552 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 553 | /* |
| 554 | * Generate an instruction without arguments. |
| 555 | * Returns a pointer to the new instruction, NULL if failed. |
| 556 | */ |
| 557 | static isn_T * |
| 558 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 559 | { |
| 560 | garray_T *instr = &cctx->ctx_instr; |
| 561 | isn_T *isn; |
| 562 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 563 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 564 | if (ga_grow(instr, 1) == FAIL) |
| 565 | return NULL; |
| 566 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 567 | isn->isn_type = isn_type; |
| 568 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 569 | ++instr->ga_len; |
| 570 | |
| 571 | return isn; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Generate an instruction without arguments. |
| 576 | * "drop" will be removed from the stack. |
| 577 | * Returns a pointer to the new instruction, NULL if failed. |
| 578 | */ |
| 579 | static isn_T * |
| 580 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 581 | { |
| 582 | garray_T *stack = &cctx->ctx_type_stack; |
| 583 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 584 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 585 | stack->ga_len -= drop; |
| 586 | return generate_instr(cctx, isn_type); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 591 | */ |
| 592 | static isn_T * |
| 593 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 594 | { |
| 595 | isn_T *isn; |
| 596 | garray_T *stack = &cctx->ctx_type_stack; |
| 597 | |
| 598 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 599 | return NULL; |
| 600 | |
| 601 | if (ga_grow(stack, 1) == FAIL) |
| 602 | return NULL; |
| 603 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 604 | ++stack->ga_len; |
| 605 | |
| 606 | return isn; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 611 | */ |
| 612 | static int |
| 613 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 614 | { |
| 615 | isn_T *isn; |
| 616 | garray_T *stack = &cctx->ctx_type_stack; |
| 617 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 618 | |
| 619 | if ((*type)->tt_type == VAR_STRING) |
| 620 | return OK; |
| 621 | *type = &t_string; |
| 622 | |
| 623 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 624 | return FAIL; |
| 625 | isn->isn_arg.number = offset; |
| 626 | |
| 627 | return OK; |
| 628 | } |
| 629 | |
| 630 | static int |
| 631 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 632 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 633 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 634 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 635 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 636 | { |
| 637 | if (*op == '+') |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 638 | emsg(_("E1035: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 639 | else |
| 640 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 641 | return FAIL; |
| 642 | } |
| 643 | return OK; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Generate an instruction with two arguments. The instruction depends on the |
| 648 | * type of the arguments. |
| 649 | */ |
| 650 | static int |
| 651 | generate_two_op(cctx_T *cctx, char_u *op) |
| 652 | { |
| 653 | garray_T *stack = &cctx->ctx_type_stack; |
| 654 | type_T *type1; |
| 655 | type_T *type2; |
| 656 | vartype_T vartype; |
| 657 | isn_T *isn; |
| 658 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 659 | RETURN_OK_IF_SKIP(cctx); |
| 660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 661 | // Get the known type of the two items on the stack. If they are matching |
| 662 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 663 | // checking. |
| 664 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 665 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 666 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 667 | if (type1->tt_type == type2->tt_type |
| 668 | && (type1->tt_type == VAR_NUMBER |
| 669 | || type1->tt_type == VAR_LIST |
| 670 | #ifdef FEAT_FLOAT |
| 671 | || type1->tt_type == VAR_FLOAT |
| 672 | #endif |
| 673 | || type1->tt_type == VAR_BLOB)) |
| 674 | vartype = type1->tt_type; |
| 675 | |
| 676 | switch (*op) |
| 677 | { |
| 678 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 679 | && type1->tt_type != VAR_ANY |
| 680 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 681 | && check_number_or_float( |
| 682 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 683 | return FAIL; |
| 684 | isn = generate_instr_drop(cctx, |
| 685 | vartype == VAR_NUMBER ? ISN_OPNR |
| 686 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 687 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 688 | #ifdef FEAT_FLOAT |
| 689 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 690 | #endif |
| 691 | : ISN_OPANY, 1); |
| 692 | if (isn != NULL) |
| 693 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 694 | break; |
| 695 | |
| 696 | case '-': |
| 697 | case '*': |
| 698 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 699 | op) == FAIL) |
| 700 | return FAIL; |
| 701 | if (vartype == VAR_NUMBER) |
| 702 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 703 | #ifdef FEAT_FLOAT |
| 704 | else if (vartype == VAR_FLOAT) |
| 705 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 706 | #endif |
| 707 | else |
| 708 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 709 | if (isn != NULL) |
| 710 | isn->isn_arg.op.op_type = *op == '*' |
| 711 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 712 | break; |
| 713 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 714 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 715 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 716 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 717 | && type2->tt_type != VAR_NUMBER)) |
| 718 | { |
| 719 | emsg(_("E1035: % requires number arguments")); |
| 720 | return FAIL; |
| 721 | } |
| 722 | isn = generate_instr_drop(cctx, |
| 723 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 724 | if (isn != NULL) |
| 725 | isn->isn_arg.op.op_type = EXPR_REM; |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 730 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 731 | { |
| 732 | type_T *type = &t_any; |
| 733 | |
| 734 | #ifdef FEAT_FLOAT |
| 735 | // float+number and number+float results in float |
| 736 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 737 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 738 | type = &t_float; |
| 739 | #endif |
| 740 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 741 | } |
| 742 | |
| 743 | return OK; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 748 | */ |
| 749 | static int |
| 750 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 751 | { |
| 752 | isntype_T isntype = ISN_DROP; |
| 753 | isn_T *isn; |
| 754 | garray_T *stack = &cctx->ctx_type_stack; |
| 755 | vartype_T type1; |
| 756 | vartype_T type2; |
| 757 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 758 | RETURN_OK_IF_SKIP(cctx); |
| 759 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 760 | // Get the known type of the two items on the stack. If they are matching |
| 761 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 762 | // checking. |
| 763 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 764 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 765 | if (type1 == VAR_UNKNOWN) |
| 766 | type1 = VAR_ANY; |
| 767 | if (type2 == VAR_UNKNOWN) |
| 768 | type2 = VAR_ANY; |
| 769 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 770 | if (type1 == type2) |
| 771 | { |
| 772 | switch (type1) |
| 773 | { |
| 774 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 775 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 776 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 777 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 778 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 779 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 780 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 781 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 782 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 783 | default: isntype = ISN_COMPAREANY; break; |
| 784 | } |
| 785 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 786 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 787 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 788 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 789 | isntype = ISN_COMPAREANY; |
| 790 | |
| 791 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 792 | && (isntype == ISN_COMPAREBOOL |
| 793 | || isntype == ISN_COMPARESPECIAL |
| 794 | || isntype == ISN_COMPARENR |
| 795 | || isntype == ISN_COMPAREFLOAT)) |
| 796 | { |
| 797 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 798 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
| 799 | return FAIL; |
| 800 | } |
| 801 | if (isntype == ISN_DROP |
| 802 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 803 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 804 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 805 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 806 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 807 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 808 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 809 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 810 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 811 | vartype_name(type1), vartype_name(type2)); |
| 812 | return FAIL; |
| 813 | } |
| 814 | |
| 815 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 816 | return FAIL; |
| 817 | isn->isn_arg.op.op_type = exptype; |
| 818 | isn->isn_arg.op.op_ic = ic; |
| 819 | |
| 820 | // takes two arguments, puts one bool back |
| 821 | if (stack->ga_len >= 2) |
| 822 | { |
| 823 | --stack->ga_len; |
| 824 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 825 | } |
| 826 | |
| 827 | return OK; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Generate an ISN_2BOOL instruction. |
| 832 | */ |
| 833 | static int |
| 834 | generate_2BOOL(cctx_T *cctx, int invert) |
| 835 | { |
| 836 | isn_T *isn; |
| 837 | garray_T *stack = &cctx->ctx_type_stack; |
| 838 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 839 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 840 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 841 | return FAIL; |
| 842 | isn->isn_arg.number = invert; |
| 843 | |
| 844 | // type becomes bool |
| 845 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 846 | |
| 847 | return OK; |
| 848 | } |
| 849 | |
| 850 | static int |
| 851 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 852 | { |
| 853 | isn_T *isn; |
| 854 | garray_T *stack = &cctx->ctx_type_stack; |
| 855 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 856 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 857 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 858 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 859 | // TODO: whole type, e.g. for a function also arg and return types |
| 860 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 861 | isn->isn_arg.type.ct_off = offset; |
| 862 | |
| 863 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 864 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 865 | |
| 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 870 | * Check that |
| 871 | * - "actual" is "expected" type or |
| 872 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 873 | * - return FAIL. |
| 874 | */ |
| 875 | static int |
| 876 | need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx) |
| 877 | { |
| 878 | if (check_type(expected, actual, FALSE) == OK) |
| 879 | return OK; |
| 880 | if (actual->tt_type != VAR_ANY |
| 881 | && actual->tt_type != VAR_UNKNOWN |
| 882 | && !(actual->tt_type == VAR_FUNC |
| 883 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 884 | { |
| 885 | type_mismatch(expected, actual); |
| 886 | return FAIL; |
| 887 | } |
| 888 | generate_TYPECHECK(cctx, expected, offset); |
| 889 | return OK; |
| 890 | } |
| 891 | |
| 892 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 893 | * Generate an ISN_PUSHNR instruction. |
| 894 | */ |
| 895 | static int |
| 896 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 897 | { |
| 898 | isn_T *isn; |
| 899 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 900 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 901 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 902 | return FAIL; |
| 903 | isn->isn_arg.number = number; |
| 904 | |
| 905 | return OK; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Generate an ISN_PUSHBOOL instruction. |
| 910 | */ |
| 911 | static int |
| 912 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 913 | { |
| 914 | isn_T *isn; |
| 915 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 916 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 917 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 918 | return FAIL; |
| 919 | isn->isn_arg.number = number; |
| 920 | |
| 921 | return OK; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Generate an ISN_PUSHSPEC instruction. |
| 926 | */ |
| 927 | static int |
| 928 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 929 | { |
| 930 | isn_T *isn; |
| 931 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 932 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 933 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 934 | return FAIL; |
| 935 | isn->isn_arg.number = number; |
| 936 | |
| 937 | return OK; |
| 938 | } |
| 939 | |
| 940 | #ifdef FEAT_FLOAT |
| 941 | /* |
| 942 | * Generate an ISN_PUSHF instruction. |
| 943 | */ |
| 944 | static int |
| 945 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 946 | { |
| 947 | isn_T *isn; |
| 948 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 949 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 950 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 951 | return FAIL; |
| 952 | isn->isn_arg.fnumber = fnumber; |
| 953 | |
| 954 | return OK; |
| 955 | } |
| 956 | #endif |
| 957 | |
| 958 | /* |
| 959 | * Generate an ISN_PUSHS instruction. |
| 960 | * Consumes "str". |
| 961 | */ |
| 962 | static int |
| 963 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 964 | { |
| 965 | isn_T *isn; |
| 966 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 967 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 968 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 969 | return FAIL; |
| 970 | isn->isn_arg.string = str; |
| 971 | |
| 972 | return OK; |
| 973 | } |
| 974 | |
| 975 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 976 | * Generate an ISN_PUSHCHANNEL instruction. |
| 977 | * Consumes "channel". |
| 978 | */ |
| 979 | static int |
| 980 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 981 | { |
| 982 | isn_T *isn; |
| 983 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 984 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 985 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 986 | return FAIL; |
| 987 | isn->isn_arg.channel = channel; |
| 988 | |
| 989 | return OK; |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * Generate an ISN_PUSHJOB instruction. |
| 994 | * Consumes "job". |
| 995 | */ |
| 996 | static int |
| 997 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 998 | { |
| 999 | isn_T *isn; |
| 1000 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1001 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1002 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1003 | return FAIL; |
| 1004 | isn->isn_arg.job = job; |
| 1005 | |
| 1006 | return OK; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1010 | * Generate an ISN_PUSHBLOB instruction. |
| 1011 | * Consumes "blob". |
| 1012 | */ |
| 1013 | static int |
| 1014 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1015 | { |
| 1016 | isn_T *isn; |
| 1017 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1018 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1019 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1020 | return FAIL; |
| 1021 | isn->isn_arg.blob = blob; |
| 1022 | |
| 1023 | return OK; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1027 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1028 | * Consumes "name". |
| 1029 | */ |
| 1030 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1031 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1032 | { |
| 1033 | isn_T *isn; |
| 1034 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1035 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1036 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1037 | return FAIL; |
| 1038 | isn->isn_arg.string = name; |
| 1039 | |
| 1040 | return OK; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1044 | * Generate an ISN_STORE instruction. |
| 1045 | */ |
| 1046 | static int |
| 1047 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1048 | { |
| 1049 | isn_T *isn; |
| 1050 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1051 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1052 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1053 | return FAIL; |
| 1054 | if (name != NULL) |
| 1055 | isn->isn_arg.string = vim_strsave(name); |
| 1056 | else |
| 1057 | isn->isn_arg.number = idx; |
| 1058 | |
| 1059 | return OK; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1064 | */ |
| 1065 | static int |
| 1066 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1067 | { |
| 1068 | isn_T *isn; |
| 1069 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1070 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1071 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1072 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1073 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1074 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1075 | |
| 1076 | return OK; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Generate an ISN_STOREOPT instruction |
| 1081 | */ |
| 1082 | static int |
| 1083 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1084 | { |
| 1085 | isn_T *isn; |
| 1086 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1087 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1088 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1089 | return FAIL; |
| 1090 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1091 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1092 | |
| 1093 | return OK; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Generate an ISN_LOAD or similar instruction. |
| 1098 | */ |
| 1099 | static int |
| 1100 | generate_LOAD( |
| 1101 | cctx_T *cctx, |
| 1102 | isntype_T isn_type, |
| 1103 | int idx, |
| 1104 | char_u *name, |
| 1105 | type_T *type) |
| 1106 | { |
| 1107 | isn_T *isn; |
| 1108 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1109 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1110 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1111 | return FAIL; |
| 1112 | if (name != NULL) |
| 1113 | isn->isn_arg.string = vim_strsave(name); |
| 1114 | else |
| 1115 | isn->isn_arg.number = idx; |
| 1116 | |
| 1117 | return OK; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1121 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1122 | */ |
| 1123 | static int |
| 1124 | generate_LOADV( |
| 1125 | cctx_T *cctx, |
| 1126 | char_u *name, |
| 1127 | int error) |
| 1128 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1129 | int di_flags; |
| 1130 | int vidx = find_vim_var(name, &di_flags); |
| 1131 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1132 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1133 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1134 | if (vidx < 0) |
| 1135 | { |
| 1136 | if (error) |
| 1137 | semsg(_(e_var_notfound), name); |
| 1138 | return FAIL; |
| 1139 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1140 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1141 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1142 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1146 | * Generate an ISN_UNLET instruction. |
| 1147 | */ |
| 1148 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1149 | generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1150 | { |
| 1151 | isn_T *isn; |
| 1152 | |
| 1153 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1154 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1155 | return FAIL; |
| 1156 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1157 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1158 | |
| 1159 | return OK; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1163 | * Generate an ISN_LOADS instruction. |
| 1164 | */ |
| 1165 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1166 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1167 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1168 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1169 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1170 | int sid, |
| 1171 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1172 | { |
| 1173 | isn_T *isn; |
| 1174 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1175 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1176 | if (isn_type == ISN_LOADS) |
| 1177 | isn = generate_instr_type(cctx, isn_type, type); |
| 1178 | else |
| 1179 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1180 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1181 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1182 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1183 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1184 | |
| 1185 | return OK; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1190 | */ |
| 1191 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1192 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1193 | cctx_T *cctx, |
| 1194 | isntype_T isn_type, |
| 1195 | int sid, |
| 1196 | int idx, |
| 1197 | type_T *type) |
| 1198 | { |
| 1199 | isn_T *isn; |
| 1200 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1201 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1202 | if (isn_type == ISN_LOADSCRIPT) |
| 1203 | isn = generate_instr_type(cctx, isn_type, type); |
| 1204 | else |
| 1205 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1206 | if (isn == NULL) |
| 1207 | return FAIL; |
| 1208 | isn->isn_arg.script.script_sid = sid; |
| 1209 | isn->isn_arg.script.script_idx = idx; |
| 1210 | return OK; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * Generate an ISN_NEWLIST instruction. |
| 1215 | */ |
| 1216 | static int |
| 1217 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1218 | { |
| 1219 | isn_T *isn; |
| 1220 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1221 | type_T *type; |
| 1222 | type_T *member; |
| 1223 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1224 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1225 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1226 | return FAIL; |
| 1227 | isn->isn_arg.number = count; |
| 1228 | |
| 1229 | // drop the value types |
| 1230 | stack->ga_len -= count; |
| 1231 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1232 | // Use the first value type for the list member type. Use "any" for an |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1233 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1234 | if (count > 0) |
| 1235 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1236 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1237 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1238 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1239 | |
| 1240 | // add the list type to the type stack |
| 1241 | if (ga_grow(stack, 1) == FAIL) |
| 1242 | return FAIL; |
| 1243 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1244 | ++stack->ga_len; |
| 1245 | |
| 1246 | return OK; |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Generate an ISN_NEWDICT instruction. |
| 1251 | */ |
| 1252 | static int |
| 1253 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1254 | { |
| 1255 | isn_T *isn; |
| 1256 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1257 | type_T *type; |
| 1258 | type_T *member; |
| 1259 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1260 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1261 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1262 | return FAIL; |
| 1263 | isn->isn_arg.number = count; |
| 1264 | |
| 1265 | // drop the key and value types |
| 1266 | stack->ga_len -= 2 * count; |
| 1267 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1268 | // Use the first value type for the list member type. Use "void" for an |
| 1269 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1270 | if (count > 0) |
| 1271 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1272 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1273 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1274 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1275 | |
| 1276 | // add the dict type to the type stack |
| 1277 | if (ga_grow(stack, 1) == FAIL) |
| 1278 | return FAIL; |
| 1279 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1280 | ++stack->ga_len; |
| 1281 | |
| 1282 | return OK; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Generate an ISN_FUNCREF instruction. |
| 1287 | */ |
| 1288 | static int |
| 1289 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1290 | { |
| 1291 | isn_T *isn; |
| 1292 | garray_T *stack = &cctx->ctx_type_stack; |
| 1293 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1294 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1295 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1296 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1297 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1298 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1299 | |
| 1300 | if (ga_grow(stack, 1) == FAIL) |
| 1301 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1302 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1303 | // TODO: argument and return types |
| 1304 | ++stack->ga_len; |
| 1305 | |
| 1306 | return OK; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Generate an ISN_JUMP instruction. |
| 1311 | */ |
| 1312 | static int |
| 1313 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1314 | { |
| 1315 | isn_T *isn; |
| 1316 | garray_T *stack = &cctx->ctx_type_stack; |
| 1317 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1318 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1319 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1320 | return FAIL; |
| 1321 | isn->isn_arg.jump.jump_when = when; |
| 1322 | isn->isn_arg.jump.jump_where = where; |
| 1323 | |
| 1324 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1325 | --stack->ga_len; |
| 1326 | |
| 1327 | return OK; |
| 1328 | } |
| 1329 | |
| 1330 | static int |
| 1331 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1332 | { |
| 1333 | isn_T *isn; |
| 1334 | garray_T *stack = &cctx->ctx_type_stack; |
| 1335 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1336 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1337 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1338 | return FAIL; |
| 1339 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1340 | |
| 1341 | if (ga_grow(stack, 1) == FAIL) |
| 1342 | return FAIL; |
| 1343 | // type doesn't matter, will be stored next |
| 1344 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1345 | ++stack->ga_len; |
| 1346 | |
| 1347 | return OK; |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * Generate an ISN_BCALL instruction. |
| 1352 | * Return FAIL if the number of arguments is wrong. |
| 1353 | */ |
| 1354 | static int |
| 1355 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount) |
| 1356 | { |
| 1357 | isn_T *isn; |
| 1358 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1359 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1360 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1361 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1362 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1363 | if (check_internal_func(func_idx, argcount) == FAIL) |
| 1364 | return FAIL; |
| 1365 | |
| 1366 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1367 | return FAIL; |
| 1368 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1369 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1370 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1371 | for (i = 0; i < argcount; ++i) |
| 1372 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1373 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1374 | stack->ga_len -= argcount; // drop the arguments |
| 1375 | if (ga_grow(stack, 1) == FAIL) |
| 1376 | return FAIL; |
| 1377 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1378 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | ++stack->ga_len; // add return value |
| 1380 | |
| 1381 | return OK; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1386 | * Return FAIL if the number of arguments is wrong. |
| 1387 | */ |
| 1388 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1389 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1390 | { |
| 1391 | isn_T *isn; |
| 1392 | garray_T *stack = &cctx->ctx_type_stack; |
| 1393 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1394 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1395 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1396 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1397 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1398 | { |
| 1399 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1400 | return FAIL; |
| 1401 | } |
| 1402 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1403 | { |
| 1404 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1405 | return FAIL; |
| 1406 | } |
| 1407 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1408 | if (ufunc->uf_dfunc_idx >= 0) |
| 1409 | { |
| 1410 | int i; |
| 1411 | |
| 1412 | for (i = 0; i < argcount; ++i) |
| 1413 | { |
| 1414 | type_T *expected; |
| 1415 | type_T *actual; |
| 1416 | |
| 1417 | if (i < regular_args) |
| 1418 | { |
| 1419 | if (ufunc->uf_arg_types == NULL) |
| 1420 | continue; |
| 1421 | expected = ufunc->uf_arg_types[i]; |
| 1422 | } |
| 1423 | else |
| 1424 | expected = ufunc->uf_va_type->tt_member; |
| 1425 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1426 | if (need_type(actual, expected, -argcount + i, cctx) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1427 | { |
| 1428 | arg_type_mismatch(expected, actual, i + 1); |
| 1429 | return FAIL; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1434 | if ((isn = generate_instr(cctx, |
| 1435 | ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL) |
| 1436 | return FAIL; |
| 1437 | if (ufunc->uf_dfunc_idx >= 0) |
| 1438 | { |
| 1439 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1440 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1441 | } |
| 1442 | else |
| 1443 | { |
| 1444 | // A user function may be deleted and redefined later, can't use the |
| 1445 | // ufunc pointer, need to look it up again at runtime. |
| 1446 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1447 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1448 | } |
| 1449 | |
| 1450 | stack->ga_len -= argcount; // drop the arguments |
| 1451 | if (ga_grow(stack, 1) == FAIL) |
| 1452 | return FAIL; |
| 1453 | // add return value |
| 1454 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1455 | ++stack->ga_len; |
| 1456 | |
| 1457 | return OK; |
| 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1462 | */ |
| 1463 | static int |
| 1464 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1465 | { |
| 1466 | isn_T *isn; |
| 1467 | garray_T *stack = &cctx->ctx_type_stack; |
| 1468 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1469 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1470 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1471 | return FAIL; |
| 1472 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1473 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1474 | |
| 1475 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1476 | if (ga_grow(stack, 1) == FAIL) |
| 1477 | return FAIL; |
| 1478 | // add return value |
| 1479 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1480 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1481 | |
| 1482 | return OK; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1487 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1488 | */ |
| 1489 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1490 | generate_PCALL( |
| 1491 | cctx_T *cctx, |
| 1492 | int argcount, |
| 1493 | char_u *name, |
| 1494 | type_T *type, |
| 1495 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1496 | { |
| 1497 | isn_T *isn; |
| 1498 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1499 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1500 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1501 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1502 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1503 | if (type->tt_type == VAR_ANY) |
| 1504 | ret_type = &t_any; |
| 1505 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1506 | { |
| 1507 | if (type->tt_argcount != -1) |
| 1508 | { |
| 1509 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1510 | |
| 1511 | if (argcount < type->tt_min_argcount - varargs) |
| 1512 | { |
| 1513 | semsg(_(e_toofewarg), "[reference]"); |
| 1514 | return FAIL; |
| 1515 | } |
| 1516 | if (!varargs && argcount > type->tt_argcount) |
| 1517 | { |
| 1518 | semsg(_(e_toomanyarg), "[reference]"); |
| 1519 | return FAIL; |
| 1520 | } |
| 1521 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1522 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1523 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1524 | else |
| 1525 | { |
| 1526 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1527 | return FAIL; |
| 1528 | } |
| 1529 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1530 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1531 | return FAIL; |
| 1532 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1533 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1534 | |
| 1535 | stack->ga_len -= argcount; // drop the arguments |
| 1536 | |
| 1537 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1538 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1539 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1540 | // If partial is above the arguments it must be cleared and replaced with |
| 1541 | // the return value. |
| 1542 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1543 | return FAIL; |
| 1544 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | return OK; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * Generate an ISN_MEMBER instruction. |
| 1550 | */ |
| 1551 | static int |
| 1552 | generate_MEMBER(cctx_T *cctx, char_u *name, size_t len) |
| 1553 | { |
| 1554 | isn_T *isn; |
| 1555 | garray_T *stack = &cctx->ctx_type_stack; |
| 1556 | type_T *type; |
| 1557 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1558 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1559 | if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL) |
| 1560 | return FAIL; |
| 1561 | isn->isn_arg.string = vim_strnsave(name, (int)len); |
| 1562 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1563 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1564 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1565 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1566 | { |
| 1567 | emsg(_(e_dictreq)); |
| 1568 | return FAIL; |
| 1569 | } |
| 1570 | // change dict type to dict member type |
| 1571 | if (type->tt_type == VAR_DICT) |
| 1572 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | |
| 1574 | return OK; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Generate an ISN_ECHO instruction. |
| 1579 | */ |
| 1580 | static int |
| 1581 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1582 | { |
| 1583 | isn_T *isn; |
| 1584 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1585 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1586 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1587 | return FAIL; |
| 1588 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1589 | isn->isn_arg.echo.echo_count = count; |
| 1590 | |
| 1591 | return OK; |
| 1592 | } |
| 1593 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1594 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1595 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1596 | */ |
| 1597 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1598 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1599 | { |
| 1600 | isn_T *isn; |
| 1601 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1602 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1603 | return FAIL; |
| 1604 | isn->isn_arg.number = count; |
| 1605 | |
| 1606 | return OK; |
| 1607 | } |
| 1608 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1609 | static int |
| 1610 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1611 | { |
| 1612 | isn_T *isn; |
| 1613 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1614 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1615 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1616 | return FAIL; |
| 1617 | isn->isn_arg.string = vim_strsave(line); |
| 1618 | return OK; |
| 1619 | } |
| 1620 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1621 | static int |
| 1622 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1623 | { |
| 1624 | isn_T *isn; |
| 1625 | |
| 1626 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1627 | return FAIL; |
| 1628 | isn->isn_arg.number = count; |
| 1629 | return OK; |
| 1630 | } |
| 1631 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1632 | /* |
| 1633 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1634 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1635 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1636 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1637 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1638 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1639 | lvar_T *lvar; |
| 1640 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1641 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1642 | { |
| 1643 | emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1644 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1648 | return NULL; |
| 1649 | 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] | 1650 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1651 | // Every local variable uses the next entry on the stack. We could re-use |
| 1652 | // the last ones when leaving a scope, but then variables used in a closure |
| 1653 | // might get overwritten. To keep things simple do not re-use stack |
| 1654 | // entries. This is less efficient, but memory is cheap these days. |
| 1655 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1656 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1657 | lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len)); |
| 1658 | lvar->lv_const = isConst; |
| 1659 | lvar->lv_type = type; |
| 1660 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1661 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1665 | * Remove local variables above "new_top". |
| 1666 | */ |
| 1667 | static void |
| 1668 | unwind_locals(cctx_T *cctx, int new_top) |
| 1669 | { |
| 1670 | if (cctx->ctx_locals.ga_len > new_top) |
| 1671 | { |
| 1672 | int idx; |
| 1673 | lvar_T *lvar; |
| 1674 | |
| 1675 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1676 | { |
| 1677 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1678 | vim_free(lvar->lv_name); |
| 1679 | } |
| 1680 | } |
| 1681 | cctx->ctx_locals.ga_len = new_top; |
| 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * Free all local variables. |
| 1686 | */ |
| 1687 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1688 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1689 | { |
| 1690 | unwind_locals(cctx, 0); |
| 1691 | ga_clear(&cctx->ctx_locals); |
| 1692 | } |
| 1693 | |
| 1694 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1695 | * Skip over a type definition and return a pointer to just after it. |
| 1696 | */ |
| 1697 | char_u * |
| 1698 | skip_type(char_u *start) |
| 1699 | { |
| 1700 | char_u *p = start; |
| 1701 | |
| 1702 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1703 | ++p; |
| 1704 | |
| 1705 | // Skip over "<type>"; this is permissive about white space. |
| 1706 | if (*skipwhite(p) == '<') |
| 1707 | { |
| 1708 | p = skipwhite(p); |
| 1709 | p = skip_type(skipwhite(p + 1)); |
| 1710 | p = skipwhite(p); |
| 1711 | if (*p == '>') |
| 1712 | ++p; |
| 1713 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1714 | else if (*p == '(' && STRNCMP("func", start, 4) == 0) |
| 1715 | { |
| 1716 | // handle func(args): type |
| 1717 | ++p; |
| 1718 | while (*p != ')' && *p != NUL) |
| 1719 | { |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1720 | char_u *sp = p; |
| 1721 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1722 | p = skip_type(p); |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1723 | if (p == sp) |
| 1724 | return p; // syntax error |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1725 | if (*p == ',') |
| 1726 | p = skipwhite(p + 1); |
| 1727 | } |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1728 | if (*p == ')') |
| 1729 | { |
| 1730 | if (p[1] == ':') |
| 1731 | p = skip_type(skipwhite(p + 2)); |
| 1732 | else |
| 1733 | p = skipwhite(p + 1); |
| 1734 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1737 | return p; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1742 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1743 | * Returns NULL in case of failure. |
| 1744 | */ |
| 1745 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1746 | 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] | 1747 | { |
| 1748 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1749 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1750 | |
| 1751 | if (**arg != '<') |
| 1752 | { |
| 1753 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1754 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1755 | else |
| 1756 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1757 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1758 | } |
| 1759 | *arg = skipwhite(*arg + 1); |
| 1760 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1761 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1762 | |
| 1763 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1764 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1765 | { |
| 1766 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1767 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1768 | } |
| 1769 | ++*arg; |
| 1770 | |
| 1771 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1772 | return get_list_type(member_type, type_gap); |
| 1773 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1778 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1779 | */ |
| 1780 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1781 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1782 | { |
| 1783 | char_u *p = *arg; |
| 1784 | size_t len; |
| 1785 | |
| 1786 | // skip over the first word |
| 1787 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1788 | ++p; |
| 1789 | len = p - *arg; |
| 1790 | |
| 1791 | switch (**arg) |
| 1792 | { |
| 1793 | case 'a': |
| 1794 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1795 | { |
| 1796 | *arg += len; |
| 1797 | return &t_any; |
| 1798 | } |
| 1799 | break; |
| 1800 | case 'b': |
| 1801 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1802 | { |
| 1803 | *arg += len; |
| 1804 | return &t_bool; |
| 1805 | } |
| 1806 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1807 | { |
| 1808 | *arg += len; |
| 1809 | return &t_blob; |
| 1810 | } |
| 1811 | break; |
| 1812 | case 'c': |
| 1813 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1814 | { |
| 1815 | *arg += len; |
| 1816 | return &t_channel; |
| 1817 | } |
| 1818 | break; |
| 1819 | case 'd': |
| 1820 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1821 | { |
| 1822 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1823 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1824 | } |
| 1825 | break; |
| 1826 | case 'f': |
| 1827 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1828 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1829 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1830 | *arg += len; |
| 1831 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1832 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1833 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1834 | return &t_any; |
| 1835 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1836 | } |
| 1837 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 1838 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1839 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1840 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1841 | int argcount = -1; |
| 1842 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1843 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1844 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 1845 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1846 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1847 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1848 | if (**arg == '(') |
| 1849 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1850 | // "func" may or may not return a value, "func()" does |
| 1851 | // not return a value. |
| 1852 | ret_type = &t_void; |
| 1853 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1854 | p = ++*arg; |
| 1855 | argcount = 0; |
| 1856 | while (*p != NUL && *p != ')') |
| 1857 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1858 | if (*p == '?') |
| 1859 | { |
| 1860 | if (first_optional == -1) |
| 1861 | first_optional = argcount; |
| 1862 | ++p; |
| 1863 | } |
| 1864 | else if (first_optional != -1) |
| 1865 | { |
| 1866 | emsg(_("E1007: mandatory argument after optional argument")); |
| 1867 | return &t_any; |
| 1868 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1869 | else if (STRNCMP(p, "...", 3) == 0) |
| 1870 | { |
| 1871 | flags |= TTFLAG_VARARGS; |
| 1872 | p += 3; |
| 1873 | } |
| 1874 | |
| 1875 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 1876 | |
| 1877 | // Nothing comes after "...{type}". |
| 1878 | if (flags & TTFLAG_VARARGS) |
| 1879 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1880 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1881 | if (*p != ',' && *skipwhite(p) == ',') |
| 1882 | { |
| 1883 | semsg(_(e_no_white_before), ","); |
| 1884 | return &t_any; |
| 1885 | } |
| 1886 | if (*p == ',') |
| 1887 | { |
| 1888 | ++p; |
| 1889 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1890 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1891 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1892 | return &t_any; |
| 1893 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1894 | } |
| 1895 | p = skipwhite(p); |
| 1896 | if (argcount == MAX_FUNC_ARGS) |
| 1897 | { |
| 1898 | emsg(_("E740: Too many argument types")); |
| 1899 | return &t_any; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | p = skipwhite(p); |
| 1904 | if (*p != ')') |
| 1905 | { |
| 1906 | emsg(_(e_missing_close)); |
| 1907 | return &t_any; |
| 1908 | } |
| 1909 | *arg = p + 1; |
| 1910 | } |
| 1911 | if (**arg == ':') |
| 1912 | { |
| 1913 | // parse return type |
| 1914 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1915 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1916 | semsg(_(e_white_after), ":"); |
| 1917 | *arg = skipwhite(*arg); |
| 1918 | ret_type = parse_type(arg, type_gap); |
| 1919 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1920 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1921 | type = get_func_type(ret_type, argcount, type_gap); |
| 1922 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1923 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1924 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 1925 | type->tt_flags = flags; |
| 1926 | if (argcount > 0) |
| 1927 | { |
| 1928 | type->tt_argcount = argcount; |
| 1929 | type->tt_min_argcount = first_optional == -1 |
| 1930 | ? argcount : first_optional; |
| 1931 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1932 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1933 | return &t_any; |
| 1934 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1935 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1936 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1937 | } |
| 1938 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1939 | } |
| 1940 | break; |
| 1941 | case 'j': |
| 1942 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 1943 | { |
| 1944 | *arg += len; |
| 1945 | return &t_job; |
| 1946 | } |
| 1947 | break; |
| 1948 | case 'l': |
| 1949 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 1950 | { |
| 1951 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1952 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1953 | } |
| 1954 | break; |
| 1955 | case 'n': |
| 1956 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 1957 | { |
| 1958 | *arg += len; |
| 1959 | return &t_number; |
| 1960 | } |
| 1961 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1962 | case 's': |
| 1963 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 1964 | { |
| 1965 | *arg += len; |
| 1966 | return &t_string; |
| 1967 | } |
| 1968 | break; |
| 1969 | case 'v': |
| 1970 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 1971 | { |
| 1972 | *arg += len; |
| 1973 | return &t_void; |
| 1974 | } |
| 1975 | break; |
| 1976 | } |
| 1977 | |
| 1978 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 1979 | return &t_any; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * Check if "type1" and "type2" are exactly the same. |
| 1984 | */ |
| 1985 | static int |
| 1986 | equal_type(type_T *type1, type_T *type2) |
| 1987 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 1988 | int i; |
| 1989 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1990 | if (type1->tt_type != type2->tt_type) |
| 1991 | return FALSE; |
| 1992 | switch (type1->tt_type) |
| 1993 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1994 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 1995 | case VAR_ANY: |
| 1996 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1997 | case VAR_SPECIAL: |
| 1998 | case VAR_BOOL: |
| 1999 | case VAR_NUMBER: |
| 2000 | case VAR_FLOAT: |
| 2001 | case VAR_STRING: |
| 2002 | case VAR_BLOB: |
| 2003 | case VAR_JOB: |
| 2004 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2005 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | case VAR_LIST: |
| 2007 | case VAR_DICT: |
| 2008 | return equal_type(type1->tt_member, type2->tt_member); |
| 2009 | case VAR_FUNC: |
| 2010 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2011 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2012 | || type1->tt_argcount != type2->tt_argcount) |
| 2013 | return FALSE; |
| 2014 | if (type1->tt_argcount < 0 |
| 2015 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2016 | return TRUE; |
| 2017 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2018 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2019 | return FALSE; |
| 2020 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2021 | } |
| 2022 | return TRUE; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2027 | * "type2" and "dest" may be the same. |
| 2028 | */ |
| 2029 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2030 | 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] | 2031 | { |
| 2032 | if (equal_type(type1, type2)) |
| 2033 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2034 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2035 | return; |
| 2036 | } |
| 2037 | |
| 2038 | if (type1->tt_type == type2->tt_type) |
| 2039 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2040 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2041 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2042 | type_T *common; |
| 2043 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2044 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2045 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2046 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2047 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2048 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2049 | return; |
| 2050 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2051 | if (type1->tt_type == VAR_FUNC) |
| 2052 | { |
| 2053 | type_T *common; |
| 2054 | |
| 2055 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2056 | if (type1->tt_argcount == type2->tt_argcount |
| 2057 | && type1->tt_argcount >= 0) |
| 2058 | { |
| 2059 | int argcount = type1->tt_argcount; |
| 2060 | int i; |
| 2061 | |
| 2062 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2063 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2064 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2065 | if (func_type_add_arg_types(*dest, argcount, |
| 2066 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2067 | for (i = 0; i < argcount; ++i) |
| 2068 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2069 | &(*dest)->tt_args[i], type_gap); |
| 2070 | } |
| 2071 | } |
| 2072 | else |
| 2073 | *dest = alloc_func_type(common, -1, type_gap); |
| 2074 | return; |
| 2075 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2076 | } |
| 2077 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2078 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | char * |
| 2082 | vartype_name(vartype_T type) |
| 2083 | { |
| 2084 | switch (type) |
| 2085 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2086 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2087 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2088 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | case VAR_SPECIAL: return "special"; |
| 2090 | case VAR_BOOL: return "bool"; |
| 2091 | case VAR_NUMBER: return "number"; |
| 2092 | case VAR_FLOAT: return "float"; |
| 2093 | case VAR_STRING: return "string"; |
| 2094 | case VAR_BLOB: return "blob"; |
| 2095 | case VAR_JOB: return "job"; |
| 2096 | case VAR_CHANNEL: return "channel"; |
| 2097 | case VAR_LIST: return "list"; |
| 2098 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2099 | |
| 2100 | case VAR_FUNC: |
| 2101 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2102 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2103 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Return the name of a type. |
| 2108 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2109 | */ |
| 2110 | char * |
| 2111 | type_name(type_T *type, char **tofree) |
| 2112 | { |
| 2113 | char *name = vartype_name(type->tt_type); |
| 2114 | |
| 2115 | *tofree = NULL; |
| 2116 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2117 | { |
| 2118 | char *member_free; |
| 2119 | char *member_name = type_name(type->tt_member, &member_free); |
| 2120 | size_t len; |
| 2121 | |
| 2122 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2123 | *tofree = alloc(len); |
| 2124 | if (*tofree != NULL) |
| 2125 | { |
| 2126 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2127 | vim_free(member_free); |
| 2128 | return *tofree; |
| 2129 | } |
| 2130 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2131 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2132 | { |
| 2133 | garray_T ga; |
| 2134 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2135 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2136 | |
| 2137 | ga_init2(&ga, 1, 100); |
| 2138 | if (ga_grow(&ga, 20) == FAIL) |
| 2139 | return "[unknown]"; |
| 2140 | *tofree = ga.ga_data; |
| 2141 | STRCPY(ga.ga_data, "func("); |
| 2142 | ga.ga_len += 5; |
| 2143 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2144 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2145 | { |
| 2146 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2147 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2148 | int len; |
| 2149 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2150 | if (type->tt_args == NULL) |
| 2151 | arg_type = "[unknown]"; |
| 2152 | else |
| 2153 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2154 | if (i > 0) |
| 2155 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2156 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2157 | ga.ga_len += 2; |
| 2158 | } |
| 2159 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2160 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2161 | { |
| 2162 | vim_free(arg_free); |
| 2163 | return "[unknown]"; |
| 2164 | } |
| 2165 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2166 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2167 | { |
| 2168 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2169 | ga.ga_len += 3; |
| 2170 | } |
| 2171 | else if (i >= type->tt_min_argcount) |
| 2172 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2173 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2174 | ga.ga_len += len; |
| 2175 | vim_free(arg_free); |
| 2176 | } |
| 2177 | |
| 2178 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2179 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2180 | else |
| 2181 | { |
| 2182 | char *ret_free; |
| 2183 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2184 | int len; |
| 2185 | |
| 2186 | len = (int)STRLEN(ret_name) + 4; |
| 2187 | if (ga_grow(&ga, len) == FAIL) |
| 2188 | { |
| 2189 | vim_free(ret_free); |
| 2190 | return "[unknown]"; |
| 2191 | } |
| 2192 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2193 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2194 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2195 | vim_free(ret_free); |
| 2196 | } |
| 2197 | return ga.ga_data; |
| 2198 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2199 | |
| 2200 | return name; |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Find "name" in script-local items of script "sid". |
| 2205 | * Returns the index in "sn_var_vals" if found. |
| 2206 | * If found but not in "sn_var_vals" returns -1. |
| 2207 | * If not found returns -2. |
| 2208 | */ |
| 2209 | int |
| 2210 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2211 | { |
| 2212 | hashtab_T *ht; |
| 2213 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2214 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2215 | int idx; |
| 2216 | |
| 2217 | // First look the name up in the hashtable. |
| 2218 | if (sid <= 0 || sid > script_items.ga_len) |
| 2219 | return -1; |
| 2220 | ht = &SCRIPT_VARS(sid); |
| 2221 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2222 | if (di == NULL) |
| 2223 | return -2; |
| 2224 | |
| 2225 | // Now find the svar_T index in sn_var_vals. |
| 2226 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2227 | { |
| 2228 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2229 | |
| 2230 | if (sv->sv_tv == &di->di_tv) |
| 2231 | { |
| 2232 | if (check_writable && sv->sv_const) |
| 2233 | semsg(_(e_readonlyvar), name); |
| 2234 | return idx; |
| 2235 | } |
| 2236 | } |
| 2237 | return -1; |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * Find "name" in imported items of the current script/ |
| 2242 | */ |
| 2243 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2244 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2245 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2246 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2247 | int idx; |
| 2248 | |
| 2249 | if (cctx != NULL) |
| 2250 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2251 | { |
| 2252 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2253 | + idx; |
| 2254 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2255 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2256 | : STRLEN(import->imp_name) == len |
| 2257 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2258 | return import; |
| 2259 | } |
| 2260 | |
| 2261 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2262 | { |
| 2263 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2264 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2265 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2266 | : STRLEN(import->imp_name) == len |
| 2267 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2268 | return import; |
| 2269 | } |
| 2270 | return NULL; |
| 2271 | } |
| 2272 | |
| 2273 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2274 | * Free all imported variables. |
| 2275 | */ |
| 2276 | static void |
| 2277 | free_imported(cctx_T *cctx) |
| 2278 | { |
| 2279 | int idx; |
| 2280 | |
| 2281 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2282 | { |
| 2283 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2284 | |
| 2285 | vim_free(import->imp_name); |
| 2286 | } |
| 2287 | ga_clear(&cctx->ctx_imports); |
| 2288 | } |
| 2289 | |
| 2290 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2291 | * Get the next line of the function from "cctx". |
| 2292 | * Returns NULL when at the end. |
| 2293 | */ |
| 2294 | static char_u * |
| 2295 | next_line_from_context(cctx_T *cctx) |
| 2296 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2297 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2298 | |
| 2299 | do |
| 2300 | { |
| 2301 | ++cctx->ctx_lnum; |
| 2302 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2303 | { |
| 2304 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2305 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2306 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2307 | 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] | 2308 | cctx->ctx_line_start = line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2309 | SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum |
| 2310 | + cctx->ctx_lnum + 1; |
Bram Moolenaar | 675f716 | 2020-04-12 22:53:54 +0200 | [diff] [blame] | 2311 | } while (line == NULL || *skipwhite(line) == NUL); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2312 | return line; |
| 2313 | } |
| 2314 | |
| 2315 | /* |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2316 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2317 | */ |
| 2318 | static int |
| 2319 | comment_start(char_u *p) |
| 2320 | { |
| 2321 | return p[0] == '#' && p[1] != '{'; |
| 2322 | } |
| 2323 | |
| 2324 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2325 | * 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] | 2326 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2327 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2328 | */ |
| 2329 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2330 | 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] | 2331 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2332 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2333 | { |
| 2334 | char_u *next = next_line_from_context(cctx); |
| 2335 | |
| 2336 | if (next == NULL) |
| 2337 | return FAIL; |
| 2338 | *arg = skipwhite(next); |
| 2339 | } |
| 2340 | return OK; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2344 | * Generate an instruction to load script-local variable "name", without the |
| 2345 | * leading "s:". |
| 2346 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2347 | */ |
| 2348 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2349 | compile_load_scriptvar( |
| 2350 | cctx_T *cctx, |
| 2351 | char_u *name, // variable NUL terminated |
| 2352 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2353 | char_u **end, // end of variable |
| 2354 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2355 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2356 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2357 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2358 | imported_T *import; |
| 2359 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2360 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2361 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2362 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2363 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2364 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2365 | } |
| 2366 | if (idx >= 0) |
| 2367 | { |
| 2368 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2369 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2370 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2371 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2372 | return OK; |
| 2373 | } |
| 2374 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2375 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2376 | if (import != NULL) |
| 2377 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2378 | if (import->imp_all) |
| 2379 | { |
| 2380 | char_u *p = skipwhite(*end); |
| 2381 | int name_len; |
| 2382 | ufunc_T *ufunc; |
| 2383 | type_T *type; |
| 2384 | |
| 2385 | // Used "import * as Name", need to lookup the member. |
| 2386 | if (*p != '.') |
| 2387 | { |
| 2388 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2389 | return FAIL; |
| 2390 | } |
| 2391 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2392 | if (VIM_ISWHITE(*p)) |
| 2393 | { |
| 2394 | emsg(_("E1074: no white space allowed after dot")); |
| 2395 | return FAIL; |
| 2396 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2397 | |
| 2398 | idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type); |
| 2399 | // TODO: what if it is a function? |
| 2400 | if (idx < 0) |
| 2401 | return FAIL; |
| 2402 | *end = p; |
| 2403 | |
| 2404 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2405 | import->imp_sid, |
| 2406 | idx, |
| 2407 | type); |
| 2408 | } |
| 2409 | else |
| 2410 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2411 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2412 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2413 | import->imp_sid, |
| 2414 | import->imp_var_vals_idx, |
| 2415 | import->imp_type); |
| 2416 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2417 | return OK; |
| 2418 | } |
| 2419 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2420 | if (error) |
| 2421 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2422 | return FAIL; |
| 2423 | } |
| 2424 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2425 | static int |
| 2426 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2427 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2428 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2429 | |
| 2430 | if (ufunc == NULL) |
| 2431 | return FAIL; |
| 2432 | |
| 2433 | return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type); |
| 2434 | } |
| 2435 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2436 | /* |
| 2437 | * Compile a variable name into a load instruction. |
| 2438 | * "end" points to just after the name. |
| 2439 | * When "error" is FALSE do not give an error when not found. |
| 2440 | */ |
| 2441 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2442 | 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] | 2443 | { |
| 2444 | type_T *type; |
| 2445 | char_u *name; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2446 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2448 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2449 | |
| 2450 | if (*(*arg + 1) == ':') |
| 2451 | { |
| 2452 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2453 | if (end <= *arg + 2) |
| 2454 | name = vim_strsave((char_u *)"[empty]"); |
| 2455 | else |
| 2456 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2457 | if (name == NULL) |
| 2458 | return FAIL; |
| 2459 | |
| 2460 | if (**arg == 'v') |
| 2461 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2462 | res = generate_LOADV(cctx, name, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2463 | } |
| 2464 | else if (**arg == 'g') |
| 2465 | { |
| 2466 | // Global variables can be defined later, thus we don't check if it |
| 2467 | // exists, give error at runtime. |
| 2468 | res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); |
| 2469 | } |
| 2470 | else if (**arg == 's') |
| 2471 | { |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2472 | res = compile_load_scriptvar(cctx, name, NULL, NULL, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2473 | } |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2474 | else if (**arg == 'b') |
| 2475 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2476 | // Buffer-local variables can be defined later, thus we don't check |
| 2477 | // if it exists, give error at runtime. |
| 2478 | res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2479 | } |
| 2480 | else if (**arg == 'w') |
| 2481 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2482 | // Window-local variables can be defined later, thus we don't check |
| 2483 | // if it exists, give error at runtime. |
| 2484 | res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2485 | } |
| 2486 | else if (**arg == 't') |
| 2487 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2488 | // Tabpage-local variables can be defined later, thus we don't |
| 2489 | // check if it exists, give error at runtime. |
| 2490 | res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2491 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2492 | else |
| 2493 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2494 | semsg("E1075: Namespace not supported: %s", *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2495 | goto theend; |
| 2496 | } |
| 2497 | } |
| 2498 | else |
| 2499 | { |
| 2500 | size_t len = end - *arg; |
| 2501 | int idx; |
| 2502 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2503 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2504 | |
| 2505 | name = vim_strnsave(*arg, end - *arg); |
| 2506 | if (name == NULL) |
| 2507 | return FAIL; |
| 2508 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2509 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2510 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2511 | if (!gen_load_outer) |
| 2512 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2513 | } |
| 2514 | else |
| 2515 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2516 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2517 | |
| 2518 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2519 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2520 | type = lvar->lv_type; |
| 2521 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2522 | if (lvar->lv_from_outer) |
| 2523 | gen_load_outer = TRUE; |
| 2524 | else |
| 2525 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2526 | } |
| 2527 | else |
| 2528 | { |
| 2529 | if ((len == 4 && STRNCMP("true", *arg, 4) == 0) |
| 2530 | || (len == 5 && STRNCMP("false", *arg, 5) == 0)) |
| 2531 | res = generate_PUSHBOOL(cctx, **arg == 't' |
| 2532 | ? VVAL_TRUE : VVAL_FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2533 | else |
| 2534 | { |
| 2535 | // "var" can be script-local even without using "s:" if it |
| 2536 | // already exists. |
| 2537 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2538 | == SCRIPT_VERSION_VIM9 |
| 2539 | || lookup_script(*arg, len) == OK) |
| 2540 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2541 | FALSE); |
| 2542 | |
| 2543 | // When the name starts with an uppercase letter or "x:" it |
| 2544 | // can be a user defined function. |
| 2545 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2546 | res = generate_funcref(cctx, name); |
| 2547 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | if (gen_load) |
| 2551 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2552 | if (gen_load_outer) |
| 2553 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | *arg = end; |
| 2557 | |
| 2558 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2559 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2560 | semsg(_(e_var_notfound), name); |
| 2561 | vim_free(name); |
| 2562 | return res; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Compile the argument expressions. |
| 2567 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2568 | */ |
| 2569 | static int |
| 2570 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2571 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2572 | char_u *p = *arg; |
| 2573 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2574 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2575 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2576 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2577 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2578 | { |
| 2579 | p = next_line_from_context(cctx); |
| 2580 | if (p == NULL) |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2581 | goto failret; |
| 2582 | whitep = (char_u *)" "; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2583 | p = skipwhite(p); |
| 2584 | } |
| 2585 | if (*p == ')') |
| 2586 | { |
| 2587 | *arg = p + 1; |
| 2588 | return OK; |
| 2589 | } |
| 2590 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2591 | if (compile_expr1(&p, cctx) == FAIL) |
| 2592 | return FAIL; |
| 2593 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2594 | |
| 2595 | if (*p != ',' && *skipwhite(p) == ',') |
| 2596 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2597 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2598 | p = skipwhite(p); |
| 2599 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2600 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2601 | { |
| 2602 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2603 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2604 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2605 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2606 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2607 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2608 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2609 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2610 | emsg(_(e_missing_close)); |
| 2611 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | /* |
| 2615 | * Compile a function call: name(arg1, arg2) |
| 2616 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2617 | * "argcount_init" is 1 for "value->method()" |
| 2618 | * Instructions: |
| 2619 | * EVAL arg1 |
| 2620 | * EVAL arg2 |
| 2621 | * BCALL / DCALL / UCALL |
| 2622 | */ |
| 2623 | static int |
| 2624 | compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init) |
| 2625 | { |
| 2626 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2627 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2628 | int argcount = argcount_init; |
| 2629 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2630 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2631 | char_u *tofree = NULL; |
| 2632 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2633 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2634 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2635 | |
| 2636 | if (varlen >= sizeof(namebuf)) |
| 2637 | { |
| 2638 | semsg(_("E1011: name too long: %s"), name); |
| 2639 | return FAIL; |
| 2640 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2641 | vim_strncpy(namebuf, *arg, varlen); |
| 2642 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2643 | |
| 2644 | *arg = skipwhite(*arg + varlen + 1); |
| 2645 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2646 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2647 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2648 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2649 | { |
| 2650 | int idx; |
| 2651 | |
| 2652 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2653 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2654 | if (idx >= 0) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2655 | res = generate_BCALL(cctx, idx, argcount); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2656 | else |
| 2657 | semsg(_(e_unknownfunc), namebuf); |
| 2658 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2659 | } |
| 2660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2661 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2662 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2663 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2664 | { |
| 2665 | res = generate_CALL(cctx, ufunc, argcount); |
| 2666 | goto theend; |
| 2667 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2668 | |
| 2669 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2670 | // 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] | 2671 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2672 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 2673 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2674 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2675 | garray_T *stack = &cctx->ctx_type_stack; |
| 2676 | type_T *type; |
| 2677 | |
| 2678 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2679 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2680 | goto theend; |
| 2681 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2682 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2683 | // 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] | 2684 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2685 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 2686 | res = generate_UCALL(cctx, name, argcount); |
| 2687 | else |
| 2688 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2689 | |
| 2690 | theend: |
| 2691 | vim_free(tofree); |
| 2692 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2696 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2697 | |
| 2698 | /* |
| 2699 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2700 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2701 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2702 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2703 | * valid name. |
| 2704 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2705 | static char_u * |
| 2706 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2707 | { |
| 2708 | char_u *p; |
| 2709 | |
| 2710 | // Quick check for valid starting character. |
| 2711 | if (!eval_isnamec1(*arg)) |
| 2712 | return arg; |
| 2713 | |
| 2714 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2715 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2716 | // and can be used in slice "[n:]". |
| 2717 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2718 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2719 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2720 | break; |
| 2721 | return p; |
| 2722 | } |
| 2723 | |
| 2724 | /* |
| 2725 | * Like to_name_end() but also skip over a list or dict constant. |
| 2726 | */ |
| 2727 | char_u * |
| 2728 | to_name_const_end(char_u *arg) |
| 2729 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2730 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2731 | typval_T rettv; |
| 2732 | |
| 2733 | if (p == arg && *arg == '[') |
| 2734 | { |
| 2735 | |
| 2736 | // Can be "[1, 2, 3]->Func()". |
| 2737 | if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL) |
| 2738 | p = arg; |
| 2739 | } |
| 2740 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2741 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2742 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2743 | ++p; |
| 2744 | if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL) |
| 2745 | p = arg; |
| 2746 | } |
| 2747 | else if (p == arg && *arg == '{') |
| 2748 | { |
| 2749 | int ret = get_lambda_tv(&p, &rettv, FALSE); |
| 2750 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2751 | // Can be "{x -> ret}()". |
| 2752 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2753 | if (ret == NOTDONE) |
| 2754 | ret = eval_dict(&p, &rettv, FALSE, FALSE); |
| 2755 | if (ret != OK) |
| 2756 | p = arg; |
| 2757 | } |
| 2758 | |
| 2759 | return p; |
| 2760 | } |
| 2761 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2762 | /* |
| 2763 | * parse a list: [expr, expr] |
| 2764 | * "*arg" points to the '['. |
| 2765 | */ |
| 2766 | static int |
| 2767 | compile_list(char_u **arg, cctx_T *cctx) |
| 2768 | { |
| 2769 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2770 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2771 | int count = 0; |
| 2772 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2773 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2774 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2775 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2776 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2777 | p = next_line_from_context(cctx); |
| 2778 | if (p == NULL) |
| 2779 | { |
| 2780 | semsg(_(e_list_end), *arg); |
| 2781 | return FAIL; |
| 2782 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2783 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2784 | p = skipwhite(p); |
| 2785 | } |
| 2786 | if (*p == ']') |
| 2787 | { |
| 2788 | ++p; |
| 2789 | // Allow for following comment, after at least one space. |
| 2790 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '"') |
| 2791 | p += STRLEN(p); |
| 2792 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2793 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2794 | if (compile_expr1(&p, cctx) == FAIL) |
| 2795 | break; |
| 2796 | ++count; |
| 2797 | if (*p == ',') |
| 2798 | ++p; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2799 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2800 | p = skipwhite(p); |
| 2801 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2802 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2803 | |
| 2804 | generate_NEWLIST(cctx, count); |
| 2805 | return OK; |
| 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * parse a lambda: {arg, arg -> expr} |
| 2810 | * "*arg" points to the '{'. |
| 2811 | */ |
| 2812 | static int |
| 2813 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2814 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2815 | typval_T rettv; |
| 2816 | ufunc_T *ufunc; |
| 2817 | |
| 2818 | // Get the funcref in "rettv". |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2819 | if (get_lambda_tv(arg, &rettv, TRUE) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2820 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2821 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2822 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2823 | ++ufunc->uf_refcount; |
| 2824 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2825 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2826 | |
| 2827 | // The function will have one line: "return {expr}". |
| 2828 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2829 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2830 | |
| 2831 | if (ufunc->uf_dfunc_idx >= 0) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 2832 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2833 | return FAIL; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * Compile a lamda call: expr->{lambda}(args) |
| 2838 | * "arg" points to the "{". |
| 2839 | */ |
| 2840 | static int |
| 2841 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2842 | { |
| 2843 | ufunc_T *ufunc; |
| 2844 | typval_T rettv; |
| 2845 | int argcount = 1; |
| 2846 | int ret = FAIL; |
| 2847 | |
| 2848 | // Get the funcref in "rettv". |
| 2849 | if (get_lambda_tv(arg, &rettv, TRUE) == FAIL) |
| 2850 | return FAIL; |
| 2851 | |
| 2852 | if (**arg != '(') |
| 2853 | { |
| 2854 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2855 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2856 | else |
| 2857 | semsg(_(e_missing_paren), "lambda"); |
| 2858 | clear_tv(&rettv); |
| 2859 | return FAIL; |
| 2860 | } |
| 2861 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2862 | ufunc = rettv.vval.v_partial->pt_func; |
| 2863 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2864 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2865 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2866 | |
| 2867 | // The function will have one line: "return {expr}". |
| 2868 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2869 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2870 | |
| 2871 | // compile the arguments |
| 2872 | *arg = skipwhite(*arg + 1); |
| 2873 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2874 | // call the compiled function |
| 2875 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2876 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2877 | return ret; |
| 2878 | } |
| 2879 | |
| 2880 | /* |
| 2881 | * parse a dict: {'key': val} or #{key: val} |
| 2882 | * "*arg" points to the '{'. |
| 2883 | */ |
| 2884 | static int |
| 2885 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2886 | { |
| 2887 | garray_T *instr = &cctx->ctx_instr; |
| 2888 | int count = 0; |
| 2889 | dict_T *d = dict_alloc(); |
| 2890 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2891 | char_u *whitep = *arg; |
| 2892 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2893 | |
| 2894 | if (d == NULL) |
| 2895 | return FAIL; |
| 2896 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2897 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2898 | { |
| 2899 | char_u *key = NULL; |
| 2900 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2901 | while (**arg == NUL || (literal && **arg == '"') |
| 2902 | || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2903 | { |
| 2904 | *arg = next_line_from_context(cctx); |
| 2905 | if (*arg == NULL) |
| 2906 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2907 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2908 | *arg = skipwhite(*arg); |
| 2909 | } |
| 2910 | |
| 2911 | if (**arg == '}') |
| 2912 | break; |
| 2913 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2914 | if (literal) |
| 2915 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2916 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2917 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2918 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2919 | { |
| 2920 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 2921 | return FAIL; |
| 2922 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2923 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2924 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2925 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2926 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2927 | } |
| 2928 | else |
| 2929 | { |
| 2930 | isn_T *isn; |
| 2931 | |
| 2932 | if (compile_expr1(arg, cctx) == FAIL) |
| 2933 | return FAIL; |
| 2934 | // TODO: check type is string |
| 2935 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2936 | if (isn->isn_type == ISN_PUSHS) |
| 2937 | key = isn->isn_arg.string; |
| 2938 | } |
| 2939 | |
| 2940 | // Check for duplicate keys, if using string keys. |
| 2941 | if (key != NULL) |
| 2942 | { |
| 2943 | item = dict_find(d, key, -1); |
| 2944 | if (item != NULL) |
| 2945 | { |
| 2946 | semsg(_(e_duplicate_key), key); |
| 2947 | goto failret; |
| 2948 | } |
| 2949 | item = dictitem_alloc(key); |
| 2950 | if (item != NULL) |
| 2951 | { |
| 2952 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2953 | item->di_tv.v_lock = 0; |
| 2954 | if (dict_add(d, item) == FAIL) |
| 2955 | dictitem_free(item); |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | *arg = skipwhite(*arg); |
| 2960 | if (**arg != ':') |
| 2961 | { |
| 2962 | semsg(_(e_missing_dict_colon), *arg); |
| 2963 | return FAIL; |
| 2964 | } |
| 2965 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2966 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2967 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2968 | while (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2969 | { |
| 2970 | *arg = next_line_from_context(cctx); |
| 2971 | if (*arg == NULL) |
| 2972 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2973 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2974 | *arg = skipwhite(*arg); |
| 2975 | } |
| 2976 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2977 | if (compile_expr1(arg, cctx) == FAIL) |
| 2978 | return FAIL; |
| 2979 | ++count; |
| 2980 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2981 | whitep = *arg; |
| 2982 | p = skipwhite(*arg); |
| 2983 | while (*p == NUL || (VIM_ISWHITE(*whitep) && comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2984 | { |
| 2985 | *arg = next_line_from_context(cctx); |
| 2986 | if (*arg == NULL) |
| 2987 | goto failret; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2988 | whitep = (char_u *)" "; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2989 | *arg = skipwhite(*arg); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2990 | p = *arg; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2991 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2992 | if (**arg == '}') |
| 2993 | break; |
| 2994 | if (**arg != ',') |
| 2995 | { |
| 2996 | semsg(_(e_missing_dict_comma), *arg); |
| 2997 | goto failret; |
| 2998 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2999 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3000 | *arg = skipwhite(*arg + 1); |
| 3001 | } |
| 3002 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3003 | *arg = *arg + 1; |
| 3004 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3005 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3006 | p = skipwhite(*arg); |
| 3007 | if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3008 | *arg += STRLEN(*arg); |
| 3009 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3010 | dict_unref(d); |
| 3011 | return generate_NEWDICT(cctx, count); |
| 3012 | |
| 3013 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3014 | if (*arg == NULL) |
| 3015 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | dict_unref(d); |
| 3017 | return FAIL; |
| 3018 | } |
| 3019 | |
| 3020 | /* |
| 3021 | * Compile "&option". |
| 3022 | */ |
| 3023 | static int |
| 3024 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3025 | { |
| 3026 | typval_T rettv; |
| 3027 | char_u *start = *arg; |
| 3028 | int ret; |
| 3029 | |
| 3030 | // parse the option and get the current value to get the type. |
| 3031 | rettv.v_type = VAR_UNKNOWN; |
| 3032 | ret = get_option_tv(arg, &rettv, TRUE); |
| 3033 | if (ret == OK) |
| 3034 | { |
| 3035 | // include the '&' in the name, get_option_tv() expects it. |
| 3036 | char_u *name = vim_strnsave(start, *arg - start); |
| 3037 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3038 | |
| 3039 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3040 | vim_free(name); |
| 3041 | } |
| 3042 | clear_tv(&rettv); |
| 3043 | |
| 3044 | return ret; |
| 3045 | } |
| 3046 | |
| 3047 | /* |
| 3048 | * Compile "$VAR". |
| 3049 | */ |
| 3050 | static int |
| 3051 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3052 | { |
| 3053 | char_u *start = *arg; |
| 3054 | int len; |
| 3055 | int ret; |
| 3056 | char_u *name; |
| 3057 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3058 | ++*arg; |
| 3059 | len = get_env_len(arg); |
| 3060 | if (len == 0) |
| 3061 | { |
| 3062 | semsg(_(e_syntax_at), start - 1); |
| 3063 | return FAIL; |
| 3064 | } |
| 3065 | |
| 3066 | // include the '$' in the name, get_env_tv() expects it. |
| 3067 | name = vim_strnsave(start, len + 1); |
| 3068 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3069 | vim_free(name); |
| 3070 | return ret; |
| 3071 | } |
| 3072 | |
| 3073 | /* |
| 3074 | * Compile "@r". |
| 3075 | */ |
| 3076 | static int |
| 3077 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3078 | { |
| 3079 | int ret; |
| 3080 | |
| 3081 | ++*arg; |
| 3082 | if (**arg == NUL) |
| 3083 | { |
| 3084 | semsg(_(e_syntax_at), *arg - 1); |
| 3085 | return FAIL; |
| 3086 | } |
| 3087 | if (!valid_yank_reg(**arg, TRUE)) |
| 3088 | { |
| 3089 | emsg_invreg(**arg); |
| 3090 | return FAIL; |
| 3091 | } |
| 3092 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3093 | ++*arg; |
| 3094 | return ret; |
| 3095 | } |
| 3096 | |
| 3097 | /* |
| 3098 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3099 | */ |
| 3100 | static int |
| 3101 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3102 | { |
| 3103 | char_u *p = end; |
| 3104 | |
| 3105 | // this works from end to start |
| 3106 | while (p > start) |
| 3107 | { |
| 3108 | --p; |
| 3109 | if (*p == '-' || *p == '+') |
| 3110 | { |
| 3111 | // only '-' has an effect, for '+' we only check the type |
| 3112 | #ifdef FEAT_FLOAT |
| 3113 | if (rettv->v_type == VAR_FLOAT) |
| 3114 | { |
| 3115 | if (*p == '-') |
| 3116 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3117 | } |
| 3118 | else |
| 3119 | #endif |
| 3120 | { |
| 3121 | varnumber_T val; |
| 3122 | int error = FALSE; |
| 3123 | |
| 3124 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3125 | // here |
| 3126 | if (check_not_string(rettv) == FAIL) |
| 3127 | return FAIL; |
| 3128 | val = tv_get_number_chk(rettv, &error); |
| 3129 | clear_tv(rettv); |
| 3130 | if (error) |
| 3131 | return FAIL; |
| 3132 | if (*p == '-') |
| 3133 | val = -val; |
| 3134 | rettv->v_type = VAR_NUMBER; |
| 3135 | rettv->vval.v_number = val; |
| 3136 | } |
| 3137 | } |
| 3138 | else |
| 3139 | { |
| 3140 | int v = tv2bool(rettv); |
| 3141 | |
| 3142 | // '!' is permissive in the type. |
| 3143 | clear_tv(rettv); |
| 3144 | rettv->v_type = VAR_BOOL; |
| 3145 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3146 | } |
| 3147 | } |
| 3148 | return OK; |
| 3149 | } |
| 3150 | |
| 3151 | /* |
| 3152 | * Recognize v: variables that are constants and set "rettv". |
| 3153 | */ |
| 3154 | static void |
| 3155 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3156 | { |
| 3157 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3158 | { |
| 3159 | rettv->v_type = VAR_BOOL; |
| 3160 | rettv->vval.v_number = VVAL_TRUE; |
| 3161 | *arg += 6; |
| 3162 | } |
| 3163 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3164 | { |
| 3165 | rettv->v_type = VAR_BOOL; |
| 3166 | rettv->vval.v_number = VVAL_FALSE; |
| 3167 | *arg += 7; |
| 3168 | } |
| 3169 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3170 | { |
| 3171 | rettv->v_type = VAR_SPECIAL; |
| 3172 | rettv->vval.v_number = VVAL_NULL; |
| 3173 | *arg += 6; |
| 3174 | } |
| 3175 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3176 | { |
| 3177 | rettv->v_type = VAR_SPECIAL; |
| 3178 | rettv->vval.v_number = VVAL_NONE; |
| 3179 | *arg += 6; |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | /* |
| 3184 | * Compile code to apply '-', '+' and '!'. |
| 3185 | */ |
| 3186 | static int |
| 3187 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3188 | { |
| 3189 | char_u *p = end; |
| 3190 | |
| 3191 | // this works from end to start |
| 3192 | while (p > start) |
| 3193 | { |
| 3194 | --p; |
| 3195 | if (*p == '-' || *p == '+') |
| 3196 | { |
| 3197 | int negate = *p == '-'; |
| 3198 | isn_T *isn; |
| 3199 | |
| 3200 | // TODO: check type |
| 3201 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3202 | { |
| 3203 | --p; |
| 3204 | if (*p == '-') |
| 3205 | negate = !negate; |
| 3206 | } |
| 3207 | // only '-' has an effect, for '+' we only check the type |
| 3208 | if (negate) |
| 3209 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3210 | else |
| 3211 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3212 | if (isn == NULL) |
| 3213 | return FAIL; |
| 3214 | } |
| 3215 | else |
| 3216 | { |
| 3217 | int invert = TRUE; |
| 3218 | |
| 3219 | while (p > start && p[-1] == '!') |
| 3220 | { |
| 3221 | --p; |
| 3222 | invert = !invert; |
| 3223 | } |
| 3224 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3225 | return FAIL; |
| 3226 | } |
| 3227 | } |
| 3228 | return OK; |
| 3229 | } |
| 3230 | |
| 3231 | /* |
| 3232 | * Compile whatever comes after "name" or "name()". |
| 3233 | */ |
| 3234 | static int |
| 3235 | compile_subscript( |
| 3236 | char_u **arg, |
| 3237 | cctx_T *cctx, |
| 3238 | char_u **start_leader, |
| 3239 | char_u *end_leader) |
| 3240 | { |
| 3241 | for (;;) |
| 3242 | { |
| 3243 | if (**arg == '(') |
| 3244 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3245 | garray_T *stack = &cctx->ctx_type_stack; |
| 3246 | type_T *type; |
| 3247 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3248 | |
| 3249 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3250 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3251 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3252 | *arg = skipwhite(*arg + 1); |
| 3253 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3254 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3255 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3256 | return FAIL; |
| 3257 | } |
| 3258 | else if (**arg == '-' && (*arg)[1] == '>') |
| 3259 | { |
| 3260 | char_u *p; |
| 3261 | |
| 3262 | // something->method() |
| 3263 | // Apply the '!', '-' and '+' first: |
| 3264 | // -1.0->func() works like (-1.0)->func() |
| 3265 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3266 | return FAIL; |
| 3267 | *start_leader = end_leader; // don't apply again later |
| 3268 | |
| 3269 | *arg = skipwhite(*arg + 2); |
| 3270 | if (**arg == '{') |
| 3271 | { |
| 3272 | // lambda call: list->{lambda} |
| 3273 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3274 | return FAIL; |
| 3275 | } |
| 3276 | else |
| 3277 | { |
| 3278 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3279 | p = *arg; |
| 3280 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3281 | p += 2; |
| 3282 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3283 | ; |
| 3284 | if (*p != '(') |
| 3285 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3286 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3287 | return FAIL; |
| 3288 | } |
| 3289 | // TODO: base value may not be the first argument |
| 3290 | if (compile_call(arg, p - *arg, cctx, 1) == FAIL) |
| 3291 | return FAIL; |
| 3292 | } |
| 3293 | } |
| 3294 | else if (**arg == '[') |
| 3295 | { |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3296 | garray_T *stack; |
| 3297 | type_T **typep; |
| 3298 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3299 | // list index: list[123] |
| 3300 | // TODO: more arguments |
| 3301 | // TODO: dict member dict['name'] |
| 3302 | *arg = skipwhite(*arg + 1); |
| 3303 | if (compile_expr1(arg, cctx) == FAIL) |
| 3304 | return FAIL; |
| 3305 | |
| 3306 | if (**arg != ']') |
| 3307 | { |
| 3308 | emsg(_(e_missbrac)); |
| 3309 | return FAIL; |
| 3310 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3311 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3312 | |
| 3313 | if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) |
| 3314 | return FAIL; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3315 | stack = &cctx->ctx_type_stack; |
| 3316 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 3317 | if ((*typep)->tt_type != VAR_LIST && *typep != &t_any) |
| 3318 | { |
| 3319 | emsg(_(e_listreq)); |
| 3320 | return FAIL; |
| 3321 | } |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 3322 | if ((*typep)->tt_type == VAR_LIST) |
| 3323 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3324 | } |
| 3325 | else if (**arg == '.' && (*arg)[1] != '.') |
| 3326 | { |
| 3327 | char_u *p; |
| 3328 | |
| 3329 | ++*arg; |
| 3330 | p = *arg; |
| 3331 | // dictionary member: dict.name |
| 3332 | if (eval_isnamec1(*p)) |
| 3333 | while (eval_isnamec(*p)) |
| 3334 | MB_PTR_ADV(p); |
| 3335 | if (p == *arg) |
| 3336 | { |
| 3337 | semsg(_(e_syntax_at), *arg); |
| 3338 | return FAIL; |
| 3339 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3340 | if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL) |
| 3341 | return FAIL; |
| 3342 | *arg = p; |
| 3343 | } |
| 3344 | else |
| 3345 | break; |
| 3346 | } |
| 3347 | |
| 3348 | // TODO - see handle_subscript(): |
| 3349 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3350 | // Don't do this when "Func" is already a partial that was bound |
| 3351 | // explicitly (pt_auto is FALSE). |
| 3352 | |
| 3353 | return OK; |
| 3354 | } |
| 3355 | |
| 3356 | /* |
| 3357 | * Compile an expression at "*p" and add instructions to "instr". |
| 3358 | * "p" is advanced until after the expression, skipping white space. |
| 3359 | * |
| 3360 | * This is the equivalent of eval1(), eval2(), etc. |
| 3361 | */ |
| 3362 | |
| 3363 | /* |
| 3364 | * number number constant |
| 3365 | * 0zFFFFFFFF Blob constant |
| 3366 | * "string" string constant |
| 3367 | * 'string' literal string constant |
| 3368 | * &option-name option value |
| 3369 | * @r register contents |
| 3370 | * identifier variable value |
| 3371 | * function() function call |
| 3372 | * $VAR environment variable |
| 3373 | * (expression) nested expression |
| 3374 | * [expr, expr] List |
| 3375 | * {key: val, key: val} Dictionary |
| 3376 | * #{key: val, key: val} Dictionary with literal keys |
| 3377 | * |
| 3378 | * Also handle: |
| 3379 | * ! in front logical NOT |
| 3380 | * - in front unary minus |
| 3381 | * + in front unary plus (ignored) |
| 3382 | * trailing (arg) funcref/partial call |
| 3383 | * trailing [] subscript in String or List |
| 3384 | * trailing .name entry in Dictionary |
| 3385 | * trailing ->name() method call |
| 3386 | */ |
| 3387 | static int |
| 3388 | compile_expr7(char_u **arg, cctx_T *cctx) |
| 3389 | { |
| 3390 | typval_T rettv; |
| 3391 | char_u *start_leader, *end_leader; |
| 3392 | int ret = OK; |
| 3393 | |
| 3394 | /* |
| 3395 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3396 | */ |
| 3397 | start_leader = *arg; |
| 3398 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3399 | *arg = skipwhite(*arg + 1); |
| 3400 | end_leader = *arg; |
| 3401 | |
| 3402 | rettv.v_type = VAR_UNKNOWN; |
| 3403 | switch (**arg) |
| 3404 | { |
| 3405 | /* |
| 3406 | * Number constant. |
| 3407 | */ |
| 3408 | case '0': // also for blob starting with 0z |
| 3409 | case '1': |
| 3410 | case '2': |
| 3411 | case '3': |
| 3412 | case '4': |
| 3413 | case '5': |
| 3414 | case '6': |
| 3415 | case '7': |
| 3416 | case '8': |
| 3417 | case '9': |
| 3418 | case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL) |
| 3419 | return FAIL; |
| 3420 | break; |
| 3421 | |
| 3422 | /* |
| 3423 | * String constant: "string". |
| 3424 | */ |
| 3425 | case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL) |
| 3426 | return FAIL; |
| 3427 | break; |
| 3428 | |
| 3429 | /* |
| 3430 | * Literal string constant: 'str''ing'. |
| 3431 | */ |
| 3432 | case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL) |
| 3433 | return FAIL; |
| 3434 | break; |
| 3435 | |
| 3436 | /* |
| 3437 | * Constant Vim variable. |
| 3438 | */ |
| 3439 | case 'v': get_vim_constant(arg, &rettv); |
| 3440 | ret = NOTDONE; |
| 3441 | break; |
| 3442 | |
| 3443 | /* |
| 3444 | * List: [expr, expr] |
| 3445 | */ |
| 3446 | case '[': ret = compile_list(arg, cctx); |
| 3447 | break; |
| 3448 | |
| 3449 | /* |
| 3450 | * Dictionary: #{key: val, key: val} |
| 3451 | */ |
| 3452 | case '#': if ((*arg)[1] == '{') |
| 3453 | { |
| 3454 | ++*arg; |
| 3455 | ret = compile_dict(arg, cctx, TRUE); |
| 3456 | } |
| 3457 | else |
| 3458 | ret = NOTDONE; |
| 3459 | break; |
| 3460 | |
| 3461 | /* |
| 3462 | * Lambda: {arg, arg -> expr} |
| 3463 | * Dictionary: {'key': val, 'key': val} |
| 3464 | */ |
| 3465 | case '{': { |
| 3466 | char_u *start = skipwhite(*arg + 1); |
| 3467 | |
| 3468 | // Find out what comes after the arguments. |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3469 | // TODO: pass getline function |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3470 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3471 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3472 | if (ret != FAIL && *start == '>') |
| 3473 | ret = compile_lambda(arg, cctx); |
| 3474 | else |
| 3475 | ret = compile_dict(arg, cctx, FALSE); |
| 3476 | } |
| 3477 | break; |
| 3478 | |
| 3479 | /* |
| 3480 | * Option value: &name |
| 3481 | */ |
| 3482 | case '&': ret = compile_get_option(arg, cctx); |
| 3483 | break; |
| 3484 | |
| 3485 | /* |
| 3486 | * Environment variable: $VAR. |
| 3487 | */ |
| 3488 | case '$': ret = compile_get_env(arg, cctx); |
| 3489 | break; |
| 3490 | |
| 3491 | /* |
| 3492 | * Register contents: @r. |
| 3493 | */ |
| 3494 | case '@': ret = compile_get_register(arg, cctx); |
| 3495 | break; |
| 3496 | /* |
| 3497 | * nested expression: (expression). |
| 3498 | */ |
| 3499 | case '(': *arg = skipwhite(*arg + 1); |
| 3500 | ret = compile_expr1(arg, cctx); // recursive! |
| 3501 | *arg = skipwhite(*arg); |
| 3502 | if (**arg == ')') |
| 3503 | ++*arg; |
| 3504 | else if (ret == OK) |
| 3505 | { |
| 3506 | emsg(_(e_missing_close)); |
| 3507 | ret = FAIL; |
| 3508 | } |
| 3509 | break; |
| 3510 | |
| 3511 | default: ret = NOTDONE; |
| 3512 | break; |
| 3513 | } |
| 3514 | if (ret == FAIL) |
| 3515 | return FAIL; |
| 3516 | |
| 3517 | if (rettv.v_type != VAR_UNKNOWN) |
| 3518 | { |
| 3519 | // apply the '!', '-' and '+' before the constant |
| 3520 | if (apply_leader(&rettv, start_leader, end_leader) == FAIL) |
| 3521 | { |
| 3522 | clear_tv(&rettv); |
| 3523 | return FAIL; |
| 3524 | } |
| 3525 | start_leader = end_leader; // don't apply again below |
| 3526 | |
| 3527 | // push constant |
| 3528 | switch (rettv.v_type) |
| 3529 | { |
| 3530 | case VAR_BOOL: |
| 3531 | generate_PUSHBOOL(cctx, rettv.vval.v_number); |
| 3532 | break; |
| 3533 | case VAR_SPECIAL: |
| 3534 | generate_PUSHSPEC(cctx, rettv.vval.v_number); |
| 3535 | break; |
| 3536 | case VAR_NUMBER: |
| 3537 | generate_PUSHNR(cctx, rettv.vval.v_number); |
| 3538 | break; |
| 3539 | #ifdef FEAT_FLOAT |
| 3540 | case VAR_FLOAT: |
| 3541 | generate_PUSHF(cctx, rettv.vval.v_float); |
| 3542 | break; |
| 3543 | #endif |
| 3544 | case VAR_BLOB: |
| 3545 | generate_PUSHBLOB(cctx, rettv.vval.v_blob); |
| 3546 | rettv.vval.v_blob = NULL; |
| 3547 | break; |
| 3548 | case VAR_STRING: |
| 3549 | generate_PUSHS(cctx, rettv.vval.v_string); |
| 3550 | rettv.vval.v_string = NULL; |
| 3551 | break; |
| 3552 | default: |
| 3553 | iemsg("constant type missing"); |
| 3554 | return FAIL; |
| 3555 | } |
| 3556 | } |
| 3557 | else if (ret == NOTDONE) |
| 3558 | { |
| 3559 | char_u *p; |
| 3560 | int r; |
| 3561 | |
| 3562 | if (!eval_isnamec1(**arg)) |
| 3563 | { |
| 3564 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3565 | return FAIL; |
| 3566 | } |
| 3567 | |
| 3568 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3569 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3570 | if (*p == '(') |
| 3571 | r = compile_call(arg, p - *arg, cctx, 0); |
| 3572 | else |
| 3573 | r = compile_load(arg, p, cctx, TRUE); |
| 3574 | if (r == FAIL) |
| 3575 | return FAIL; |
| 3576 | } |
| 3577 | |
| 3578 | if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL) |
| 3579 | return FAIL; |
| 3580 | |
| 3581 | // Now deal with prefixed '-', '+' and '!', if not done already. |
| 3582 | return compile_leader(cctx, start_leader, end_leader); |
| 3583 | } |
| 3584 | |
| 3585 | /* |
| 3586 | * * number multiplication |
| 3587 | * / number division |
| 3588 | * % number modulo |
| 3589 | */ |
| 3590 | static int |
| 3591 | compile_expr6(char_u **arg, cctx_T *cctx) |
| 3592 | { |
| 3593 | char_u *op; |
| 3594 | |
| 3595 | // get the first variable |
| 3596 | if (compile_expr7(arg, cctx) == FAIL) |
| 3597 | return FAIL; |
| 3598 | |
| 3599 | /* |
| 3600 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3601 | */ |
| 3602 | for (;;) |
| 3603 | { |
| 3604 | op = skipwhite(*arg); |
| 3605 | if (*op != '*' && *op != '/' && *op != '%') |
| 3606 | break; |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3607 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3608 | { |
| 3609 | char_u buf[3]; |
| 3610 | |
| 3611 | vim_strncpy(buf, op, 1); |
| 3612 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3613 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3614 | } |
| 3615 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3616 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3617 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3618 | |
| 3619 | // get the second variable |
| 3620 | if (compile_expr7(arg, cctx) == FAIL) |
| 3621 | return FAIL; |
| 3622 | |
| 3623 | generate_two_op(cctx, op); |
| 3624 | } |
| 3625 | |
| 3626 | return OK; |
| 3627 | } |
| 3628 | |
| 3629 | /* |
| 3630 | * + number addition |
| 3631 | * - number subtraction |
| 3632 | * .. string concatenation |
| 3633 | */ |
| 3634 | static int |
| 3635 | compile_expr5(char_u **arg, cctx_T *cctx) |
| 3636 | { |
| 3637 | char_u *op; |
| 3638 | int oplen; |
| 3639 | |
| 3640 | // get the first variable |
| 3641 | if (compile_expr6(arg, cctx) == FAIL) |
| 3642 | return FAIL; |
| 3643 | |
| 3644 | /* |
| 3645 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3646 | */ |
| 3647 | for (;;) |
| 3648 | { |
| 3649 | op = skipwhite(*arg); |
| 3650 | if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.'))) |
| 3651 | break; |
| 3652 | oplen = (*op == '.' ? 2 : 1); |
| 3653 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3654 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3655 | { |
| 3656 | char_u buf[3]; |
| 3657 | |
| 3658 | vim_strncpy(buf, op, oplen); |
| 3659 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3660 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3664 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3665 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3666 | |
| 3667 | // get the second variable |
| 3668 | if (compile_expr6(arg, cctx) == FAIL) |
| 3669 | return FAIL; |
| 3670 | |
| 3671 | if (*op == '.') |
| 3672 | { |
| 3673 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3674 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3675 | return FAIL; |
| 3676 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3677 | } |
| 3678 | else |
| 3679 | generate_two_op(cctx, op); |
| 3680 | } |
| 3681 | |
| 3682 | return OK; |
| 3683 | } |
| 3684 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3685 | static exptype_T |
| 3686 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3687 | { |
| 3688 | exptype_T type = EXPR_UNKNOWN; |
| 3689 | int i; |
| 3690 | |
| 3691 | switch (p[0]) |
| 3692 | { |
| 3693 | case '=': if (p[1] == '=') |
| 3694 | type = EXPR_EQUAL; |
| 3695 | else if (p[1] == '~') |
| 3696 | type = EXPR_MATCH; |
| 3697 | break; |
| 3698 | case '!': if (p[1] == '=') |
| 3699 | type = EXPR_NEQUAL; |
| 3700 | else if (p[1] == '~') |
| 3701 | type = EXPR_NOMATCH; |
| 3702 | break; |
| 3703 | case '>': if (p[1] != '=') |
| 3704 | { |
| 3705 | type = EXPR_GREATER; |
| 3706 | *len = 1; |
| 3707 | } |
| 3708 | else |
| 3709 | type = EXPR_GEQUAL; |
| 3710 | break; |
| 3711 | case '<': if (p[1] != '=') |
| 3712 | { |
| 3713 | type = EXPR_SMALLER; |
| 3714 | *len = 1; |
| 3715 | } |
| 3716 | else |
| 3717 | type = EXPR_SEQUAL; |
| 3718 | break; |
| 3719 | case 'i': if (p[1] == 's') |
| 3720 | { |
| 3721 | // "is" and "isnot"; but not a prefix of a name |
| 3722 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3723 | *len = 5; |
| 3724 | i = p[*len]; |
| 3725 | if (!isalnum(i) && i != '_') |
| 3726 | { |
| 3727 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3728 | *type_is = TRUE; |
| 3729 | } |
| 3730 | } |
| 3731 | break; |
| 3732 | } |
| 3733 | return type; |
| 3734 | } |
| 3735 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3736 | /* |
| 3737 | * expr5a == expr5b |
| 3738 | * expr5a =~ expr5b |
| 3739 | * expr5a != expr5b |
| 3740 | * expr5a !~ expr5b |
| 3741 | * expr5a > expr5b |
| 3742 | * expr5a >= expr5b |
| 3743 | * expr5a < expr5b |
| 3744 | * expr5a <= expr5b |
| 3745 | * expr5a is expr5b |
| 3746 | * expr5a isnot expr5b |
| 3747 | * |
| 3748 | * Produces instructions: |
| 3749 | * EVAL expr5a Push result of "expr5a" |
| 3750 | * EVAL expr5b Push result of "expr5b" |
| 3751 | * COMPARE one of the compare instructions |
| 3752 | */ |
| 3753 | static int |
| 3754 | compile_expr4(char_u **arg, cctx_T *cctx) |
| 3755 | { |
| 3756 | exptype_T type = EXPR_UNKNOWN; |
| 3757 | char_u *p; |
| 3758 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | int type_is = FALSE; |
| 3760 | |
| 3761 | // get the first variable |
| 3762 | if (compile_expr5(arg, cctx) == FAIL) |
| 3763 | return FAIL; |
| 3764 | |
| 3765 | p = skipwhite(*arg); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3766 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | |
| 3768 | /* |
| 3769 | * If there is a comparative operator, use it. |
| 3770 | */ |
| 3771 | if (type != EXPR_UNKNOWN) |
| 3772 | { |
| 3773 | int ic = FALSE; // Default: do not ignore case |
| 3774 | |
| 3775 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3776 | { |
| 3777 | semsg(_(e_invexpr2), *arg); |
| 3778 | return FAIL; |
| 3779 | } |
| 3780 | // extra question mark appended: ignore case |
| 3781 | if (p[len] == '?') |
| 3782 | { |
| 3783 | ic = TRUE; |
| 3784 | ++len; |
| 3785 | } |
| 3786 | // extra '#' appended: match case (ignored) |
| 3787 | else if (p[len] == '#') |
| 3788 | ++len; |
| 3789 | // nothing appended: match case |
| 3790 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3791 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | { |
| 3793 | char_u buf[7]; |
| 3794 | |
| 3795 | vim_strncpy(buf, p, len); |
| 3796 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3797 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | // get the second variable |
| 3801 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3802 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3803 | return FAIL; |
| 3804 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3805 | if (compile_expr5(arg, cctx) == FAIL) |
| 3806 | return FAIL; |
| 3807 | |
| 3808 | generate_COMPARE(cctx, type, ic); |
| 3809 | } |
| 3810 | |
| 3811 | return OK; |
| 3812 | } |
| 3813 | |
| 3814 | /* |
| 3815 | * Compile || or &&. |
| 3816 | */ |
| 3817 | static int |
| 3818 | compile_and_or(char_u **arg, cctx_T *cctx, char *op) |
| 3819 | { |
| 3820 | char_u *p = skipwhite(*arg); |
| 3821 | int opchar = *op; |
| 3822 | |
| 3823 | if (p[0] == opchar && p[1] == opchar) |
| 3824 | { |
| 3825 | garray_T *instr = &cctx->ctx_instr; |
| 3826 | garray_T end_ga; |
| 3827 | |
| 3828 | /* |
| 3829 | * Repeat until there is no following "||" or "&&" |
| 3830 | */ |
| 3831 | ga_init2(&end_ga, sizeof(int), 10); |
| 3832 | while (p[0] == opchar && p[1] == opchar) |
| 3833 | { |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3834 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3835 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3836 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3837 | return FAIL; |
| 3838 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3839 | |
| 3840 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3841 | { |
| 3842 | ga_clear(&end_ga); |
| 3843 | return FAIL; |
| 3844 | } |
| 3845 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3846 | ++end_ga.ga_len; |
| 3847 | generate_JUMP(cctx, opchar == '|' |
| 3848 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3849 | |
| 3850 | // eval the next expression |
| 3851 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3852 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3853 | return FAIL; |
| 3854 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3855 | if ((opchar == '|' ? compile_expr3(arg, cctx) |
| 3856 | : compile_expr4(arg, cctx)) == FAIL) |
| 3857 | { |
| 3858 | ga_clear(&end_ga); |
| 3859 | return FAIL; |
| 3860 | } |
| 3861 | p = skipwhite(*arg); |
| 3862 | } |
| 3863 | |
| 3864 | // Fill in the end label in all jumps. |
| 3865 | while (end_ga.ga_len > 0) |
| 3866 | { |
| 3867 | isn_T *isn; |
| 3868 | |
| 3869 | --end_ga.ga_len; |
| 3870 | isn = ((isn_T *)instr->ga_data) |
| 3871 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3872 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3873 | } |
| 3874 | ga_clear(&end_ga); |
| 3875 | } |
| 3876 | |
| 3877 | return OK; |
| 3878 | } |
| 3879 | |
| 3880 | /* |
| 3881 | * expr4a && expr4a && expr4a logical AND |
| 3882 | * |
| 3883 | * Produces instructions: |
| 3884 | * EVAL expr4a Push result of "expr4a" |
| 3885 | * JUMP_AND_KEEP_IF_FALSE end |
| 3886 | * EVAL expr4b Push result of "expr4b" |
| 3887 | * JUMP_AND_KEEP_IF_FALSE end |
| 3888 | * EVAL expr4c Push result of "expr4c" |
| 3889 | * end: |
| 3890 | */ |
| 3891 | static int |
| 3892 | compile_expr3(char_u **arg, cctx_T *cctx) |
| 3893 | { |
| 3894 | // get the first variable |
| 3895 | if (compile_expr4(arg, cctx) == FAIL) |
| 3896 | return FAIL; |
| 3897 | |
| 3898 | // || and && work almost the same |
| 3899 | return compile_and_or(arg, cctx, "&&"); |
| 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * expr3a || expr3b || expr3c logical OR |
| 3904 | * |
| 3905 | * Produces instructions: |
| 3906 | * EVAL expr3a Push result of "expr3a" |
| 3907 | * JUMP_AND_KEEP_IF_TRUE end |
| 3908 | * EVAL expr3b Push result of "expr3b" |
| 3909 | * JUMP_AND_KEEP_IF_TRUE end |
| 3910 | * EVAL expr3c Push result of "expr3c" |
| 3911 | * end: |
| 3912 | */ |
| 3913 | static int |
| 3914 | compile_expr2(char_u **arg, cctx_T *cctx) |
| 3915 | { |
| 3916 | // eval the first expression |
| 3917 | if (compile_expr3(arg, cctx) == FAIL) |
| 3918 | return FAIL; |
| 3919 | |
| 3920 | // || and && work almost the same |
| 3921 | return compile_and_or(arg, cctx, "||"); |
| 3922 | } |
| 3923 | |
| 3924 | /* |
| 3925 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 3926 | * |
| 3927 | * Produces instructions: |
| 3928 | * EVAL expr2 Push result of "expr" |
| 3929 | * JUMP_IF_FALSE alt jump if false |
| 3930 | * EVAL expr1a |
| 3931 | * JUMP_ALWAYS end |
| 3932 | * alt: EVAL expr1b |
| 3933 | * end: |
| 3934 | */ |
| 3935 | static int |
| 3936 | compile_expr1(char_u **arg, cctx_T *cctx) |
| 3937 | { |
| 3938 | char_u *p; |
| 3939 | |
Bram Moolenaar | 3df02f5 | 2020-05-03 15:47:33 +0200 | [diff] [blame] | 3940 | // TODO: Try parsing as a constant. If that works just one PUSH |
| 3941 | // instruction needs to be generated. |
| 3942 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3943 | // evaluate the first expression |
| 3944 | if (compile_expr2(arg, cctx) == FAIL) |
| 3945 | return FAIL; |
| 3946 | |
| 3947 | p = skipwhite(*arg); |
| 3948 | if (*p == '?') |
| 3949 | { |
| 3950 | garray_T *instr = &cctx->ctx_instr; |
| 3951 | garray_T *stack = &cctx->ctx_type_stack; |
| 3952 | int alt_idx = instr->ga_len; |
| 3953 | int end_idx; |
| 3954 | isn_T *isn; |
| 3955 | type_T *type1; |
| 3956 | type_T *type2; |
| 3957 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3958 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3959 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3960 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3961 | return FAIL; |
| 3962 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3963 | |
| 3964 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 3965 | |
| 3966 | // evaluate the second expression; any type is accepted |
| 3967 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3968 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3969 | return FAIL; |
| 3970 | |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3971 | if (compile_expr1(arg, cctx) == FAIL) |
| 3972 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3973 | |
| 3974 | // remember the type and drop it |
| 3975 | --stack->ga_len; |
| 3976 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 3977 | |
| 3978 | end_idx = instr->ga_len; |
| 3979 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 3980 | |
| 3981 | // jump here from JUMP_IF_FALSE |
| 3982 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 3983 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3984 | |
| 3985 | // Check for the ":". |
| 3986 | p = skipwhite(*arg); |
| 3987 | if (*p != ':') |
| 3988 | { |
| 3989 | emsg(_(e_missing_colon)); |
| 3990 | return FAIL; |
| 3991 | } |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3992 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3993 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3994 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3995 | return FAIL; |
| 3996 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3997 | |
| 3998 | // evaluate the third expression |
| 3999 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4000 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4001 | return FAIL; |
| 4002 | |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4003 | if (compile_expr1(arg, cctx) == FAIL) |
| 4004 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4005 | |
| 4006 | // If the types differ, the result has a more generic type. |
| 4007 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 4008 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4009 | |
| 4010 | // jump here from JUMP_ALWAYS |
| 4011 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4012 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4013 | } |
| 4014 | return OK; |
| 4015 | } |
| 4016 | |
| 4017 | /* |
| 4018 | * compile "return [expr]" |
| 4019 | */ |
| 4020 | static char_u * |
| 4021 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4022 | { |
| 4023 | char_u *p = arg; |
| 4024 | garray_T *stack = &cctx->ctx_type_stack; |
| 4025 | type_T *stack_type; |
| 4026 | |
| 4027 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4028 | { |
| 4029 | // compile return argument into instructions |
| 4030 | if (compile_expr1(&p, cctx) == FAIL) |
| 4031 | return NULL; |
| 4032 | |
| 4033 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4034 | if (set_return_type) |
| 4035 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
| 4036 | else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx) |
| 4037 | == FAIL) |
| 4038 | return NULL; |
| 4039 | } |
| 4040 | else |
| 4041 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4042 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4043 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4044 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4045 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4046 | { |
| 4047 | emsg(_("E1003: Missing return value")); |
| 4048 | return NULL; |
| 4049 | } |
| 4050 | |
| 4051 | // No argument, return zero. |
| 4052 | generate_PUSHNR(cctx, 0); |
| 4053 | } |
| 4054 | |
| 4055 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4056 | return NULL; |
| 4057 | |
| 4058 | // "return val | endif" is possible |
| 4059 | return skipwhite(p); |
| 4060 | } |
| 4061 | |
| 4062 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4063 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4064 | * Return a pointer to the line in allocated memory. |
| 4065 | * Return NULL for end-of-file or some error. |
| 4066 | */ |
| 4067 | static char_u * |
| 4068 | exarg_getline( |
| 4069 | int c UNUSED, |
| 4070 | void *cookie, |
| 4071 | int indent UNUSED, |
| 4072 | int do_concat UNUSED) |
| 4073 | { |
| 4074 | cctx_T *cctx = (cctx_T *)cookie; |
| 4075 | |
| 4076 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4077 | { |
| 4078 | iemsg("Heredoc got to end"); |
| 4079 | return NULL; |
| 4080 | } |
| 4081 | ++cctx->ctx_lnum; |
| 4082 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4083 | [cctx->ctx_lnum]); |
| 4084 | } |
| 4085 | |
| 4086 | /* |
| 4087 | * Compile a nested :def command. |
| 4088 | */ |
| 4089 | static char_u * |
| 4090 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4091 | { |
| 4092 | char_u *name_start = eap->arg; |
| 4093 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4094 | char_u *name = get_lambda_name(); |
| 4095 | lvar_T *lvar; |
| 4096 | ufunc_T *ufunc; |
| 4097 | |
| 4098 | eap->arg = name_end; |
| 4099 | eap->getline = exarg_getline; |
| 4100 | eap->cookie = cctx; |
| 4101 | eap->skip = cctx->ctx_skip == TRUE; |
| 4102 | eap->forceit = FALSE; |
| 4103 | ufunc = def_function(eap, name, cctx); |
| 4104 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4105 | if (ufunc == NULL || ufunc->uf_dfunc_idx < 0) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4106 | return NULL; |
| 4107 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4108 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4109 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4110 | TRUE, ufunc->uf_func_type); |
| 4111 | |
| 4112 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4113 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4114 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4115 | |
| 4116 | // TODO: warning for trailing? |
| 4117 | return (char_u *)""; |
| 4118 | } |
| 4119 | |
| 4120 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4121 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4122 | */ |
| 4123 | int |
| 4124 | assignment_len(char_u *p, int *heredoc) |
| 4125 | { |
| 4126 | if (*p == '=') |
| 4127 | { |
| 4128 | if (p[1] == '<' && p[2] == '<') |
| 4129 | { |
| 4130 | *heredoc = TRUE; |
| 4131 | return 3; |
| 4132 | } |
| 4133 | return 1; |
| 4134 | } |
| 4135 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4136 | return 2; |
| 4137 | if (STRNCMP(p, "..=", 3) == 0) |
| 4138 | return 3; |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | // words that cannot be used as a variable |
| 4143 | static char *reserved[] = { |
| 4144 | "true", |
| 4145 | "false", |
| 4146 | NULL |
| 4147 | }; |
| 4148 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4149 | typedef enum { |
| 4150 | dest_local, |
| 4151 | dest_option, |
| 4152 | dest_env, |
| 4153 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4154 | dest_buffer, |
| 4155 | dest_window, |
| 4156 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4157 | dest_vimvar, |
| 4158 | dest_script, |
| 4159 | dest_reg, |
| 4160 | } assign_dest_T; |
| 4161 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4162 | /* |
| 4163 | * compile "let var [= expr]", "const var = expr" and "var = expr" |
| 4164 | * "arg" points to "var". |
| 4165 | */ |
| 4166 | static char_u * |
| 4167 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4168 | { |
| 4169 | char_u *p; |
| 4170 | char_u *ret = NULL; |
| 4171 | int var_count = 0; |
| 4172 | int semicolon = 0; |
| 4173 | size_t varlen; |
| 4174 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4175 | int new_local = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4176 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4177 | int opt_type; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4178 | assign_dest_T dest = dest_local; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | int opt_flags = 0; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 4180 | int vimvaridx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | int oplen = 0; |
| 4182 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4183 | type_T *type = &t_any; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4184 | lvar_T *lvar = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4185 | char_u *name; |
| 4186 | char_u *sp; |
| 4187 | int has_type = FALSE; |
| 4188 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
| 4189 | int instr_count = -1; |
| 4190 | |
| 4191 | p = skip_var_list(arg, FALSE, &var_count, &semicolon); |
| 4192 | if (p == NULL) |
| 4193 | return NULL; |
| 4194 | if (var_count > 0) |
| 4195 | { |
| 4196 | // TODO: let [var, var] = list |
| 4197 | emsg("Cannot handle a list yet"); |
| 4198 | return NULL; |
| 4199 | } |
| 4200 | |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4201 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4202 | if (is_decl && p == arg + 2 && p[-1] == ':') |
| 4203 | --p; |
| 4204 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | varlen = p - arg; |
| 4206 | name = vim_strnsave(arg, (int)varlen); |
| 4207 | if (name == NULL) |
| 4208 | return NULL; |
| 4209 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4210 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4211 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4212 | if (*arg == '&') |
| 4213 | { |
| 4214 | int cc; |
| 4215 | long numval; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4216 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4217 | dest = dest_option; |
| 4218 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4219 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4220 | emsg(_(e_const_option)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4221 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4222 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4223 | if (is_decl) |
| 4224 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4225 | semsg(_("E1052: Cannot declare an option: %s"), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | goto theend; |
| 4227 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4228 | p = arg; |
| 4229 | p = find_option_end(&p, &opt_flags); |
| 4230 | if (p == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4231 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4232 | // cannot happen? |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4233 | emsg(_(e_letunexp)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4234 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4235 | } |
| 4236 | cc = *p; |
| 4237 | *p = NUL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4238 | opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4239 | *p = cc; |
| 4240 | if (opt_type == -3) |
| 4241 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4242 | semsg(_(e_unknown_option), arg); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4243 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4244 | } |
| 4245 | if (opt_type == -2 || opt_type == 0) |
| 4246 | type = &t_string; |
| 4247 | else |
| 4248 | type = &t_number; // both number and boolean option |
| 4249 | } |
| 4250 | else if (*arg == '$') |
| 4251 | { |
| 4252 | dest = dest_env; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4253 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4254 | if (is_decl) |
| 4255 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4256 | semsg(_("E1065: Cannot declare an environment variable: %s"), |
| 4257 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4258 | goto theend; |
| 4259 | } |
| 4260 | } |
| 4261 | else if (*arg == '@') |
| 4262 | { |
| 4263 | if (!valid_yank_reg(arg[1], TRUE)) |
| 4264 | { |
| 4265 | emsg_invreg(arg[1]); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4266 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4267 | } |
| 4268 | dest = dest_reg; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4269 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4270 | if (is_decl) |
| 4271 | { |
| 4272 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 4273 | goto theend; |
| 4274 | } |
| 4275 | } |
| 4276 | else if (STRNCMP(arg, "g:", 2) == 0) |
| 4277 | { |
| 4278 | dest = dest_global; |
| 4279 | if (is_decl) |
| 4280 | { |
| 4281 | semsg(_("E1016: Cannot declare a global variable: %s"), name); |
| 4282 | goto theend; |
| 4283 | } |
| 4284 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4285 | else if (STRNCMP(arg, "b:", 2) == 0) |
| 4286 | { |
| 4287 | dest = dest_buffer; |
| 4288 | if (is_decl) |
| 4289 | { |
| 4290 | semsg(_("E1078: Cannot declare a buffer variable: %s"), name); |
| 4291 | goto theend; |
| 4292 | } |
| 4293 | } |
| 4294 | else if (STRNCMP(arg, "w:", 2) == 0) |
| 4295 | { |
| 4296 | dest = dest_window; |
| 4297 | if (is_decl) |
| 4298 | { |
| 4299 | semsg(_("E1079: Cannot declare a window variable: %s"), name); |
| 4300 | goto theend; |
| 4301 | } |
| 4302 | } |
| 4303 | else if (STRNCMP(arg, "t:", 2) == 0) |
| 4304 | { |
| 4305 | dest = dest_tab; |
| 4306 | if (is_decl) |
| 4307 | { |
| 4308 | semsg(_("E1080: Cannot declare a tab variable: %s"), name); |
| 4309 | goto theend; |
| 4310 | } |
| 4311 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4312 | else if (STRNCMP(arg, "v:", 2) == 0) |
| 4313 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4314 | typval_T *vtv; |
| 4315 | int di_flags; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4316 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4317 | vimvaridx = find_vim_var(name + 2, &di_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4318 | if (vimvaridx < 0) |
| 4319 | { |
| 4320 | semsg(_(e_var_notfound), arg); |
| 4321 | goto theend; |
| 4322 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4323 | // We use the current value of "sandbox" here, is that OK? |
| 4324 | if (var_check_ro(di_flags, name, FALSE)) |
| 4325 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4326 | dest = dest_vimvar; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4327 | vtv = get_vim_var_tv(vimvaridx); |
| 4328 | type = typval2type(vtv); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4329 | if (is_decl) |
| 4330 | { |
| 4331 | semsg(_("E1064: Cannot declare a v: variable: %s"), name); |
| 4332 | goto theend; |
| 4333 | } |
| 4334 | } |
| 4335 | else |
| 4336 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4337 | int idx; |
| 4338 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4339 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4340 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4341 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4342 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4343 | goto theend; |
| 4344 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4345 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4346 | lvar = lookup_local(arg, varlen, cctx); |
| 4347 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4348 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4349 | if (is_decl) |
| 4350 | { |
| 4351 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4352 | goto theend; |
| 4353 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4354 | else if (lvar->lv_const) |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4355 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 4356 | semsg(_("E1018: Cannot assign to a constant: %s"), name); |
| 4357 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | else if (STRNCMP(arg, "s:", 2) == 0 |
| 4361 | || lookup_script(arg, varlen) == OK |
| 4362 | || find_imported(arg, varlen, cctx) != NULL) |
| 4363 | { |
| 4364 | dest = dest_script; |
| 4365 | if (is_decl) |
| 4366 | { |
| 4367 | semsg(_("E1054: Variable already declared in the script: %s"), |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4368 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4369 | goto theend; |
| 4370 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4371 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 4372 | else if (name[1] == ':' && name[2] != NUL) |
| 4373 | { |
| 4374 | semsg(_("E1082: Cannot use a namespaced variable: %s"), name); |
| 4375 | goto theend; |
| 4376 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4377 | } |
| 4378 | } |
| 4379 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4380 | if (dest != dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4381 | { |
| 4382 | if (is_decl && *p == ':') |
| 4383 | { |
| 4384 | // parse optional type: "let var: type = expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 4385 | if (!VIM_ISWHITE(p[1])) |
| 4386 | { |
| 4387 | semsg(_(e_white_after), ":"); |
| 4388 | goto theend; |
| 4389 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4390 | p = skipwhite(p + 1); |
| 4391 | type = parse_type(&p, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4392 | has_type = TRUE; |
| 4393 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4394 | else if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4395 | type = lvar->lv_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | sp = p; |
| 4399 | p = skipwhite(p); |
| 4400 | op = p; |
| 4401 | oplen = assignment_len(p, &heredoc); |
| 4402 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4403 | { |
| 4404 | char_u buf[4]; |
| 4405 | |
| 4406 | vim_strncpy(buf, op, oplen); |
| 4407 | semsg(_(e_white_both), buf); |
| 4408 | } |
| 4409 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4410 | if (oplen == 3 && !heredoc && dest != dest_global |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4411 | && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4412 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 4413 | emsg(_("E1019: Can only concatenate to string")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4414 | goto theend; |
| 4415 | } |
| 4416 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4417 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4418 | { |
| 4419 | if (oplen > 1 && !heredoc) |
| 4420 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4421 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4422 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4423 | name); |
| 4424 | goto theend; |
| 4425 | } |
| 4426 | |
| 4427 | // new local variable |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 4428 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4429 | goto theend; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4430 | lvar = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type); |
| 4431 | if (lvar == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4432 | goto theend; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4433 | new_local = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | if (heredoc) |
| 4437 | { |
| 4438 | list_T *l; |
| 4439 | listitem_T *li; |
| 4440 | |
| 4441 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4442 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4443 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4444 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4445 | |
| 4446 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4447 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4448 | { |
| 4449 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4450 | li->li_tv.vval.v_string = NULL; |
| 4451 | } |
| 4452 | generate_NEWLIST(cctx, l->lv_len); |
| 4453 | type = &t_list_string; |
| 4454 | list_free(l); |
| 4455 | p += STRLEN(p); |
| 4456 | } |
| 4457 | else if (oplen > 0) |
| 4458 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4459 | int r; |
| 4460 | type_T *stacktype; |
| 4461 | garray_T *stack; |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4462 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4463 | // for "+=", "*=", "..=" etc. first load the current value |
| 4464 | if (*op != '=') |
| 4465 | { |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4466 | switch (dest) |
| 4467 | { |
| 4468 | case dest_option: |
| 4469 | // TODO: check the option exists |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4470 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4471 | break; |
| 4472 | case dest_global: |
| 4473 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4474 | break; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4475 | case dest_buffer: |
| 4476 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4477 | break; |
| 4478 | case dest_window: |
| 4479 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4480 | break; |
| 4481 | case dest_tab: |
| 4482 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4483 | break; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4484 | case dest_script: |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 4485 | compile_load_scriptvar(cctx, |
| 4486 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4487 | break; |
| 4488 | case dest_env: |
| 4489 | // Include $ in the name here |
| 4490 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4491 | break; |
| 4492 | case dest_reg: |
| 4493 | generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string); |
| 4494 | break; |
| 4495 | case dest_vimvar: |
| 4496 | generate_LOADV(cctx, name + 2, TRUE); |
| 4497 | break; |
| 4498 | case dest_local: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 4499 | if (lvar->lv_from_outer) |
| 4500 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4501 | NULL, type); |
| 4502 | else |
| 4503 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4504 | break; |
| 4505 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4506 | } |
| 4507 | |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4508 | // Compile the expression. Temporarily hide the new local variable |
| 4509 | // here, it is not available to this expression. |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4510 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4511 | --cctx->ctx_locals.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4512 | instr_count = instr->ga_len; |
| 4513 | p = skipwhite(p + oplen); |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4514 | r = compile_expr1(&p, cctx); |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4515 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4516 | ++cctx->ctx_locals.ga_len; |
| 4517 | if (r == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4518 | goto theend; |
| 4519 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4520 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4521 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4522 | stack = &cctx->ctx_type_stack; |
| 4523 | stacktype = stack->ga_len == 0 ? &t_void |
| 4524 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4525 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4526 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4527 | if (new_local && !has_type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4528 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4529 | if (stacktype->tt_type == VAR_VOID) |
| 4530 | { |
| 4531 | emsg(_("E1031: Cannot use void value")); |
| 4532 | goto theend; |
| 4533 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4534 | else |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4535 | { |
| 4536 | // An empty list or dict has a &t_void member, for a |
| 4537 | // variable that implies &t_any. |
| 4538 | if (stacktype == &t_list_empty) |
| 4539 | lvar->lv_type = &t_list_any; |
| 4540 | else if (stacktype == &t_dict_empty) |
| 4541 | lvar->lv_type = &t_dict_any; |
| 4542 | else |
| 4543 | lvar->lv_type = stacktype; |
| 4544 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4545 | } |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4546 | else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL) |
| 4547 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4548 | } |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4549 | else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4550 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | else if (cmdidx == CMD_const) |
| 4554 | { |
| 4555 | emsg(_("E1021: const requires a value")); |
| 4556 | goto theend; |
| 4557 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4558 | else if (!has_type || dest == dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4559 | { |
| 4560 | emsg(_("E1022: type or initialization required")); |
| 4561 | goto theend; |
| 4562 | } |
| 4563 | else |
| 4564 | { |
| 4565 | // variables are always initialized |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4566 | if (ga_grow(instr, 1) == FAIL) |
| 4567 | goto theend; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4568 | switch (type->tt_type) |
| 4569 | { |
| 4570 | case VAR_BOOL: |
| 4571 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 4572 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4573 | case VAR_FLOAT: |
| 4574 | #ifdef FEAT_FLOAT |
| 4575 | generate_PUSHF(cctx, 0.0); |
| 4576 | #endif |
| 4577 | break; |
| 4578 | case VAR_STRING: |
| 4579 | generate_PUSHS(cctx, NULL); |
| 4580 | break; |
| 4581 | case VAR_BLOB: |
| 4582 | generate_PUSHBLOB(cctx, NULL); |
| 4583 | break; |
| 4584 | case VAR_FUNC: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4585 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4586 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4587 | case VAR_LIST: |
| 4588 | generate_NEWLIST(cctx, 0); |
| 4589 | break; |
| 4590 | case VAR_DICT: |
| 4591 | generate_NEWDICT(cctx, 0); |
| 4592 | break; |
| 4593 | case VAR_JOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 4594 | generate_PUSHJOB(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4595 | break; |
| 4596 | case VAR_CHANNEL: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 4597 | generate_PUSHCHANNEL(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4598 | break; |
| 4599 | case VAR_NUMBER: |
| 4600 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4601 | case VAR_ANY: |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 4602 | case VAR_PARTIAL: |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4603 | case VAR_VOID: |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 4604 | case VAR_SPECIAL: // cannot happen |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4605 | generate_PUSHNR(cctx, 0); |
| 4606 | break; |
| 4607 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | if (oplen > 0 && *op != '=') |
| 4611 | { |
| 4612 | type_T *expected = &t_number; |
| 4613 | garray_T *stack = &cctx->ctx_type_stack; |
| 4614 | type_T *stacktype; |
| 4615 | |
| 4616 | // TODO: if type is known use float or any operation |
| 4617 | |
| 4618 | if (*op == '.') |
| 4619 | expected = &t_string; |
| 4620 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4621 | if (need_type(stacktype, expected, -1, cctx) == FAIL) |
| 4622 | goto theend; |
| 4623 | |
| 4624 | if (*op == '.') |
| 4625 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4626 | else |
| 4627 | { |
| 4628 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 4629 | |
| 4630 | if (isn == NULL) |
| 4631 | goto theend; |
| 4632 | switch (*op) |
| 4633 | { |
| 4634 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 4635 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 4636 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 4637 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 4638 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 4639 | } |
| 4640 | } |
| 4641 | } |
| 4642 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4643 | switch (dest) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4644 | { |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4645 | case dest_option: |
| 4646 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 4647 | break; |
| 4648 | case dest_global: |
| 4649 | // include g: with the name, easier to execute that way |
| 4650 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 4651 | break; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4652 | case dest_buffer: |
| 4653 | // include b: with the name, easier to execute that way |
| 4654 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 4655 | break; |
| 4656 | case dest_window: |
| 4657 | // include w: with the name, easier to execute that way |
| 4658 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 4659 | break; |
| 4660 | case dest_tab: |
| 4661 | // include t: with the name, easier to execute that way |
| 4662 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 4663 | break; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4664 | case dest_env: |
| 4665 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 4666 | break; |
| 4667 | case dest_reg: |
| 4668 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 4669 | break; |
| 4670 | case dest_vimvar: |
| 4671 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 4672 | break; |
| 4673 | case dest_script: |
| 4674 | { |
| 4675 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4676 | imported_T *import = NULL; |
| 4677 | int sid = current_sctx.sc_sid; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4678 | int idx; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 4679 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4680 | if (name[1] != ':') |
| 4681 | { |
| 4682 | import = find_imported(name, 0, cctx); |
| 4683 | if (import != NULL) |
| 4684 | sid = import->imp_sid; |
| 4685 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4686 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4687 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 4688 | // TODO: specific type |
| 4689 | if (idx < 0) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4690 | { |
| 4691 | char_u *name_s = name; |
| 4692 | |
| 4693 | // Include s: in the name for store_var() |
| 4694 | if (name[1] != ':') |
| 4695 | { |
| 4696 | int len = (int)STRLEN(name) + 3; |
| 4697 | |
| 4698 | name_s = alloc(len); |
| 4699 | if (name_s == NULL) |
| 4700 | name_s = name; |
| 4701 | else |
| 4702 | vim_snprintf((char *)name_s, len, "s:%s", name); |
| 4703 | } |
| 4704 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any); |
| 4705 | if (name_s != name) |
| 4706 | vim_free(name_s); |
| 4707 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4708 | else |
| 4709 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 4710 | sid, idx, &t_any); |
| 4711 | } |
| 4712 | break; |
| 4713 | case dest_local: |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4714 | if (lvar != NULL) |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4715 | { |
| 4716 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4717 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4718 | // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE |
| 4719 | // into ISN_STORENR |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 4720 | if (!lvar->lv_from_outer && instr->ga_len == instr_count + 1 |
| 4721 | && isn->isn_type == ISN_PUSHNR) |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4722 | { |
| 4723 | varnumber_T val = isn->isn_arg.number; |
| 4724 | garray_T *stack = &cctx->ctx_type_stack; |
| 4725 | |
| 4726 | isn->isn_type = ISN_STORENR; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4727 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 4728 | isn->isn_arg.storenr.stnr_val = val; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4729 | if (stack->ga_len > 0) |
| 4730 | --stack->ga_len; |
| 4731 | } |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 4732 | else if (lvar->lv_from_outer) |
| 4733 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4734 | else |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 4735 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4736 | } |
| 4737 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4738 | } |
| 4739 | ret = p; |
| 4740 | |
| 4741 | theend: |
| 4742 | vim_free(name); |
| 4743 | return ret; |
| 4744 | } |
| 4745 | |
| 4746 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 4747 | * Check if "name" can be "unlet". |
| 4748 | */ |
| 4749 | int |
| 4750 | check_vim9_unlet(char_u *name) |
| 4751 | { |
| 4752 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 4753 | { |
| 4754 | semsg(_("E1081: Cannot unlet %s"), name); |
| 4755 | return FAIL; |
| 4756 | } |
| 4757 | return OK; |
| 4758 | } |
| 4759 | |
| 4760 | /* |
| 4761 | * Callback passed to ex_unletlock(). |
| 4762 | */ |
| 4763 | static int |
| 4764 | compile_unlet( |
| 4765 | lval_T *lvp, |
| 4766 | char_u *name_end, |
| 4767 | exarg_T *eap, |
| 4768 | int deep UNUSED, |
| 4769 | void *coookie) |
| 4770 | { |
| 4771 | cctx_T *cctx = coookie; |
| 4772 | |
| 4773 | if (lvp->ll_tv == NULL) |
| 4774 | { |
| 4775 | char_u *p = lvp->ll_name; |
| 4776 | int cc = *name_end; |
| 4777 | int ret = OK; |
| 4778 | |
| 4779 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 4780 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 4781 | if (*p == '$') |
| 4782 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 4783 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 4784 | ret = FAIL; |
| 4785 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 4786 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 4787 | |
| 4788 | *name_end = cc; |
| 4789 | return ret; |
| 4790 | } |
| 4791 | |
| 4792 | // TODO: unlet {list}[idx] |
| 4793 | // TODO: unlet {dict}[key] |
| 4794 | emsg("Sorry, :unlet not fully implemented yet"); |
| 4795 | return FAIL; |
| 4796 | } |
| 4797 | |
| 4798 | /* |
| 4799 | * compile "unlet var", "lock var" and "unlock var" |
| 4800 | * "arg" points to "var". |
| 4801 | */ |
| 4802 | static char_u * |
| 4803 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 4804 | { |
| 4805 | char_u *p = arg; |
| 4806 | |
| 4807 | if (eap->cmdidx != CMD_unlet) |
| 4808 | { |
| 4809 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 4810 | return NULL; |
| 4811 | } |
| 4812 | |
| 4813 | if (*p == '!') |
| 4814 | { |
| 4815 | p = skipwhite(p + 1); |
| 4816 | eap->forceit = TRUE; |
| 4817 | } |
| 4818 | |
| 4819 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 4820 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 4821 | } |
| 4822 | |
| 4823 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4824 | * Compile an :import command. |
| 4825 | */ |
| 4826 | static char_u * |
| 4827 | compile_import(char_u *arg, cctx_T *cctx) |
| 4828 | { |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 4829 | return handle_import(arg, &cctx->ctx_imports, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /* |
| 4833 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 4834 | */ |
| 4835 | static int |
| 4836 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 4837 | { |
| 4838 | garray_T *instr = &cctx->ctx_instr; |
| 4839 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 4840 | |
| 4841 | if (endlabel == NULL) |
| 4842 | return FAIL; |
| 4843 | endlabel->el_next = *el; |
| 4844 | *el = endlabel; |
| 4845 | endlabel->el_end_label = instr->ga_len; |
| 4846 | |
| 4847 | generate_JUMP(cctx, when, 0); |
| 4848 | return OK; |
| 4849 | } |
| 4850 | |
| 4851 | static void |
| 4852 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 4853 | { |
| 4854 | garray_T *instr = &cctx->ctx_instr; |
| 4855 | |
| 4856 | while (*el != NULL) |
| 4857 | { |
| 4858 | endlabel_T *cur = (*el); |
| 4859 | isn_T *isn; |
| 4860 | |
| 4861 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 4862 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4863 | *el = cur->el_next; |
| 4864 | vim_free(cur); |
| 4865 | } |
| 4866 | } |
| 4867 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 4868 | static void |
| 4869 | compile_free_jump_to_end(endlabel_T **el) |
| 4870 | { |
| 4871 | while (*el != NULL) |
| 4872 | { |
| 4873 | endlabel_T *cur = (*el); |
| 4874 | |
| 4875 | *el = cur->el_next; |
| 4876 | vim_free(cur); |
| 4877 | } |
| 4878 | } |
| 4879 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4880 | /* |
| 4881 | * Create a new scope and set up the generic items. |
| 4882 | */ |
| 4883 | static scope_T * |
| 4884 | new_scope(cctx_T *cctx, scopetype_T type) |
| 4885 | { |
| 4886 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 4887 | |
| 4888 | if (scope == NULL) |
| 4889 | return NULL; |
| 4890 | scope->se_outer = cctx->ctx_scope; |
| 4891 | cctx->ctx_scope = scope; |
| 4892 | scope->se_type = type; |
| 4893 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 4894 | return scope; |
| 4895 | } |
| 4896 | |
| 4897 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4898 | * Free the current scope and go back to the outer scope. |
| 4899 | */ |
| 4900 | static void |
| 4901 | drop_scope(cctx_T *cctx) |
| 4902 | { |
| 4903 | scope_T *scope = cctx->ctx_scope; |
| 4904 | |
| 4905 | if (scope == NULL) |
| 4906 | { |
| 4907 | iemsg("calling drop_scope() without a scope"); |
| 4908 | return; |
| 4909 | } |
| 4910 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 4911 | switch (scope->se_type) |
| 4912 | { |
| 4913 | case IF_SCOPE: |
| 4914 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 4915 | case FOR_SCOPE: |
| 4916 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 4917 | case WHILE_SCOPE: |
| 4918 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 4919 | case TRY_SCOPE: |
| 4920 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 4921 | case NO_SCOPE: |
| 4922 | case BLOCK_SCOPE: |
| 4923 | break; |
| 4924 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4925 | vim_free(scope); |
| 4926 | } |
| 4927 | |
| 4928 | /* |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4929 | * Evaluate an expression that is a constant: |
| 4930 | * has(arg) |
| 4931 | * |
| 4932 | * Also handle: |
| 4933 | * ! in front logical NOT |
| 4934 | * |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4935 | * Return FAIL if the expression is not a constant. |
| 4936 | */ |
| 4937 | static int |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4938 | evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4939 | { |
| 4940 | typval_T argvars[2]; |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4941 | char_u *start_leader, *end_leader; |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4942 | int has_call = FALSE; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4943 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4944 | /* |
| 4945 | * Skip '!' characters. They are handled later. |
| 4946 | */ |
| 4947 | start_leader = *arg; |
| 4948 | while (**arg == '!') |
| 4949 | *arg = skipwhite(*arg + 1); |
| 4950 | end_leader = *arg; |
| 4951 | |
| 4952 | /* |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4953 | * Recognize only a few types of constants for now. |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4954 | */ |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4955 | if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4])) |
| 4956 | { |
| 4957 | tv->v_type = VAR_SPECIAL; |
| 4958 | tv->vval.v_number = VVAL_TRUE; |
| 4959 | *arg += 4; |
| 4960 | return OK; |
| 4961 | } |
| 4962 | if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5])) |
| 4963 | { |
| 4964 | tv->v_type = VAR_SPECIAL; |
| 4965 | tv->vval.v_number = VVAL_FALSE; |
| 4966 | *arg += 5; |
| 4967 | return OK; |
| 4968 | } |
| 4969 | |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4970 | if (STRNCMP("has(", *arg, 4) == 0) |
| 4971 | { |
| 4972 | has_call = TRUE; |
| 4973 | *arg = skipwhite(*arg + 4); |
| 4974 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4975 | |
| 4976 | if (**arg == '"') |
| 4977 | { |
| 4978 | if (get_string_tv(arg, tv, TRUE) == FAIL) |
| 4979 | return FAIL; |
| 4980 | } |
| 4981 | else if (**arg == '\'') |
| 4982 | { |
| 4983 | if (get_lit_string_tv(arg, tv, TRUE) == FAIL) |
| 4984 | return FAIL; |
| 4985 | } |
| 4986 | else |
| 4987 | return FAIL; |
| 4988 | |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4989 | if (has_call) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4990 | { |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4991 | *arg = skipwhite(*arg); |
| 4992 | if (**arg != ')') |
| 4993 | return FAIL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 4994 | *arg = *arg + 1; |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4995 | |
| 4996 | argvars[0] = *tv; |
| 4997 | argvars[1].v_type = VAR_UNKNOWN; |
| 4998 | tv->v_type = VAR_NUMBER; |
| 4999 | tv->vval.v_number = 0; |
| 5000 | f_has(argvars, tv); |
| 5001 | clear_tv(&argvars[0]); |
| 5002 | |
| 5003 | while (start_leader < end_leader) |
| 5004 | { |
| 5005 | if (*start_leader == '!') |
| 5006 | tv->vval.v_number = !tv->vval.v_number; |
| 5007 | ++start_leader; |
| 5008 | } |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 5009 | } |
| 5010 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5011 | return OK; |
| 5012 | } |
| 5013 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 5014 | static int |
| 5015 | evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv) |
| 5016 | { |
| 5017 | exptype_T type = EXPR_UNKNOWN; |
| 5018 | char_u *p; |
| 5019 | int len = 2; |
| 5020 | int type_is = FALSE; |
| 5021 | |
| 5022 | // get the first variable |
| 5023 | if (evaluate_const_expr7(arg, cctx, tv) == FAIL) |
| 5024 | return FAIL; |
| 5025 | |
| 5026 | p = skipwhite(*arg); |
| 5027 | type = get_compare_type(p, &len, &type_is); |
| 5028 | |
| 5029 | /* |
| 5030 | * If there is a comparative operator, use it. |
| 5031 | */ |
| 5032 | if (type != EXPR_UNKNOWN) |
| 5033 | { |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 5034 | typval_T tv2; |
| 5035 | char_u *s1, *s2; |
| 5036 | char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; |
| 5037 | int n; |
| 5038 | |
| 5039 | // TODO: Only string == string is supported now |
| 5040 | if (tv->v_type != VAR_STRING) |
| 5041 | return FAIL; |
| 5042 | if (type != EXPR_EQUAL) |
| 5043 | return FAIL; |
| 5044 | |
| 5045 | // get the second variable |
Bram Moolenaar | 4227c78 | 2020-04-02 16:00:04 +0200 | [diff] [blame] | 5046 | init_tv(&tv2); |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 5047 | *arg = skipwhite(p + len); |
| 5048 | if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL |
| 5049 | || tv2.v_type != VAR_STRING) |
| 5050 | { |
| 5051 | clear_tv(&tv2); |
| 5052 | return FAIL; |
| 5053 | } |
| 5054 | s1 = tv_get_string_buf(tv, buf1); |
| 5055 | s2 = tv_get_string_buf(&tv2, buf2); |
| 5056 | n = STRCMP(s1, s2); |
| 5057 | clear_tv(tv); |
| 5058 | clear_tv(&tv2); |
| 5059 | tv->v_type = VAR_BOOL; |
| 5060 | tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | return OK; |
| 5064 | } |
| 5065 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5066 | static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv); |
| 5067 | |
| 5068 | /* |
| 5069 | * Compile constant || or &&. |
| 5070 | */ |
| 5071 | static int |
| 5072 | evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv) |
| 5073 | { |
| 5074 | char_u *p = skipwhite(*arg); |
| 5075 | int opchar = *op; |
| 5076 | |
| 5077 | if (p[0] == opchar && p[1] == opchar) |
| 5078 | { |
| 5079 | int val = tv2bool(tv); |
| 5080 | |
| 5081 | /* |
| 5082 | * Repeat until there is no following "||" or "&&" |
| 5083 | */ |
| 5084 | while (p[0] == opchar && p[1] == opchar) |
| 5085 | { |
| 5086 | typval_T tv2; |
| 5087 | |
| 5088 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2])) |
| 5089 | return FAIL; |
| 5090 | |
| 5091 | // eval the next expression |
| 5092 | *arg = skipwhite(p + 2); |
| 5093 | tv2.v_type = VAR_UNKNOWN; |
Bram Moolenaar | eed3571 | 2020-02-04 23:08:14 +0100 | [diff] [blame] | 5094 | tv2.v_lock = 0; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5095 | if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2) |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 5096 | : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5097 | { |
| 5098 | clear_tv(&tv2); |
| 5099 | return FAIL; |
| 5100 | } |
| 5101 | if ((opchar == '&') == val) |
| 5102 | { |
| 5103 | // false || tv2 or true && tv2: use tv2 |
| 5104 | clear_tv(tv); |
| 5105 | *tv = tv2; |
| 5106 | val = tv2bool(tv); |
| 5107 | } |
| 5108 | else |
| 5109 | clear_tv(&tv2); |
| 5110 | p = skipwhite(*arg); |
| 5111 | } |
| 5112 | } |
| 5113 | |
| 5114 | return OK; |
| 5115 | } |
| 5116 | |
| 5117 | /* |
| 5118 | * Evaluate an expression that is a constant: expr4 && expr4 && expr4 |
| 5119 | * Return FAIL if the expression is not a constant. |
| 5120 | */ |
| 5121 | static int |
| 5122 | evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 5123 | { |
| 5124 | // evaluate the first expression |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 5125 | if (evaluate_const_expr4(arg, cctx, tv) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5126 | return FAIL; |
| 5127 | |
| 5128 | // || and && work almost the same |
| 5129 | return evaluate_const_and_or(arg, cctx, "&&", tv); |
| 5130 | } |
| 5131 | |
| 5132 | /* |
| 5133 | * Evaluate an expression that is a constant: expr3 || expr3 || expr3 |
| 5134 | * Return FAIL if the expression is not a constant. |
| 5135 | */ |
| 5136 | static int |
| 5137 | evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 5138 | { |
| 5139 | // evaluate the first expression |
| 5140 | if (evaluate_const_expr3(arg, cctx, tv) == FAIL) |
| 5141 | return FAIL; |
| 5142 | |
| 5143 | // || and && work almost the same |
| 5144 | return evaluate_const_and_or(arg, cctx, "||", tv); |
| 5145 | } |
| 5146 | |
| 5147 | /* |
| 5148 | * Evaluate an expression that is a constant: expr2 ? expr1 : expr1 |
| 5149 | * E.g. for "has('feature')". |
| 5150 | * This does not produce error messages. "tv" should be cleared afterwards. |
| 5151 | * Return FAIL if the expression is not a constant. |
| 5152 | */ |
| 5153 | static int |
| 5154 | evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 5155 | { |
| 5156 | char_u *p; |
| 5157 | |
| 5158 | // evaluate the first expression |
| 5159 | if (evaluate_const_expr2(arg, cctx, tv) == FAIL) |
| 5160 | return FAIL; |
| 5161 | |
| 5162 | p = skipwhite(*arg); |
| 5163 | if (*p == '?') |
| 5164 | { |
| 5165 | int val = tv2bool(tv); |
| 5166 | typval_T tv2; |
| 5167 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5168 | // require space before and after the ? |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5169 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 5170 | return FAIL; |
| 5171 | |
| 5172 | // evaluate the second expression; any type is accepted |
| 5173 | clear_tv(tv); |
| 5174 | *arg = skipwhite(p + 1); |
| 5175 | if (evaluate_const_expr1(arg, cctx, tv) == FAIL) |
| 5176 | return FAIL; |
| 5177 | |
| 5178 | // Check for the ":". |
| 5179 | p = skipwhite(*arg); |
| 5180 | if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 5181 | return FAIL; |
| 5182 | |
| 5183 | // evaluate the third expression |
| 5184 | *arg = skipwhite(p + 1); |
| 5185 | tv2.v_type = VAR_UNKNOWN; |
| 5186 | if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL) |
| 5187 | { |
| 5188 | clear_tv(&tv2); |
| 5189 | return FAIL; |
| 5190 | } |
| 5191 | if (val) |
| 5192 | { |
| 5193 | // use the expr after "?" |
| 5194 | clear_tv(&tv2); |
| 5195 | } |
| 5196 | else |
| 5197 | { |
| 5198 | // use the expr after ":" |
| 5199 | clear_tv(tv); |
| 5200 | *tv = tv2; |
| 5201 | } |
| 5202 | } |
| 5203 | return OK; |
| 5204 | } |
| 5205 | |
| 5206 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5207 | * compile "if expr" |
| 5208 | * |
| 5209 | * "if expr" Produces instructions: |
| 5210 | * EVAL expr Push result of "expr" |
| 5211 | * JUMP_IF_FALSE end |
| 5212 | * ... body ... |
| 5213 | * end: |
| 5214 | * |
| 5215 | * "if expr | else" Produces instructions: |
| 5216 | * EVAL expr Push result of "expr" |
| 5217 | * JUMP_IF_FALSE else |
| 5218 | * ... body ... |
| 5219 | * JUMP_ALWAYS end |
| 5220 | * else: |
| 5221 | * ... body ... |
| 5222 | * end: |
| 5223 | * |
| 5224 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5225 | * EVAL expr Push result of "expr" |
| 5226 | * JUMP_IF_FALSE elseif |
| 5227 | * ... body ... |
| 5228 | * JUMP_ALWAYS end |
| 5229 | * elseif: |
| 5230 | * EVAL expr Push result of "expr" |
| 5231 | * JUMP_IF_FALSE else |
| 5232 | * ... body ... |
| 5233 | * JUMP_ALWAYS end |
| 5234 | * else: |
| 5235 | * ... body ... |
| 5236 | * end: |
| 5237 | */ |
| 5238 | static char_u * |
| 5239 | compile_if(char_u *arg, cctx_T *cctx) |
| 5240 | { |
| 5241 | char_u *p = arg; |
| 5242 | garray_T *instr = &cctx->ctx_instr; |
| 5243 | scope_T *scope; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5244 | typval_T tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5245 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5246 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 5247 | tv.v_type = VAR_UNKNOWN; |
| 5248 | if (evaluate_const_expr1(&p, cctx, &tv) == OK) |
| 5249 | cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE; |
| 5250 | else |
| 5251 | cctx->ctx_skip = MAYBE; |
| 5252 | clear_tv(&tv); |
| 5253 | if (cctx->ctx_skip == MAYBE) |
| 5254 | { |
| 5255 | p = arg; |
| 5256 | if (compile_expr1(&p, cctx) == FAIL) |
| 5257 | return NULL; |
| 5258 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5259 | |
| 5260 | scope = new_scope(cctx, IF_SCOPE); |
| 5261 | if (scope == NULL) |
| 5262 | return NULL; |
| 5263 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5264 | if (cctx->ctx_skip == MAYBE) |
| 5265 | { |
| 5266 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5267 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5268 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5269 | } |
| 5270 | else |
| 5271 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5272 | |
| 5273 | return p; |
| 5274 | } |
| 5275 | |
| 5276 | static char_u * |
| 5277 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5278 | { |
| 5279 | char_u *p = arg; |
| 5280 | garray_T *instr = &cctx->ctx_instr; |
| 5281 | isn_T *isn; |
| 5282 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5283 | typval_T tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5284 | |
| 5285 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5286 | { |
| 5287 | emsg(_(e_elseif_without_if)); |
| 5288 | return NULL; |
| 5289 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5290 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5291 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5292 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5293 | { |
| 5294 | 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] | 5295 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5296 | return NULL; |
| 5297 | // previous "if" or "elseif" jumps here |
| 5298 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5299 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5300 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5301 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5302 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 5303 | tv.v_type = VAR_UNKNOWN; |
| 5304 | if (evaluate_const_expr1(&p, cctx, &tv) == OK) |
| 5305 | cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE; |
| 5306 | else |
| 5307 | cctx->ctx_skip = MAYBE; |
| 5308 | clear_tv(&tv); |
| 5309 | if (cctx->ctx_skip == MAYBE) |
| 5310 | { |
| 5311 | p = arg; |
| 5312 | if (compile_expr1(&p, cctx) == FAIL) |
| 5313 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5314 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5315 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5316 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5317 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5318 | } |
| 5319 | else |
| 5320 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5321 | |
| 5322 | return p; |
| 5323 | } |
| 5324 | |
| 5325 | static char_u * |
| 5326 | compile_else(char_u *arg, cctx_T *cctx) |
| 5327 | { |
| 5328 | char_u *p = arg; |
| 5329 | garray_T *instr = &cctx->ctx_instr; |
| 5330 | isn_T *isn; |
| 5331 | scope_T *scope = cctx->ctx_scope; |
| 5332 | |
| 5333 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5334 | { |
| 5335 | emsg(_(e_else_without_if)); |
| 5336 | return NULL; |
| 5337 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5338 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5339 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5340 | // jump from previous block to the end, unless the else block is empty |
| 5341 | if (cctx->ctx_skip == MAYBE) |
| 5342 | { |
| 5343 | 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] | 5344 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5345 | return NULL; |
| 5346 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5347 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5348 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5349 | { |
| 5350 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5351 | { |
| 5352 | // previous "if" or "elseif" jumps here |
| 5353 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5354 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 5355 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | if (cctx->ctx_skip != MAYBE) |
| 5360 | cctx->ctx_skip = !cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5361 | |
| 5362 | return p; |
| 5363 | } |
| 5364 | |
| 5365 | static char_u * |
| 5366 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5367 | { |
| 5368 | scope_T *scope = cctx->ctx_scope; |
| 5369 | ifscope_T *ifscope; |
| 5370 | garray_T *instr = &cctx->ctx_instr; |
| 5371 | isn_T *isn; |
| 5372 | |
| 5373 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5374 | { |
| 5375 | emsg(_(e_endif_without_if)); |
| 5376 | return NULL; |
| 5377 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5378 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5379 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5380 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5381 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5382 | { |
| 5383 | // previous "if" or "elseif" jumps here |
| 5384 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5385 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5386 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5387 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5388 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5389 | cctx->ctx_skip = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5390 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5391 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5392 | return arg; |
| 5393 | } |
| 5394 | |
| 5395 | /* |
| 5396 | * compile "for var in expr" |
| 5397 | * |
| 5398 | * Produces instructions: |
| 5399 | * PUSHNR -1 |
| 5400 | * STORE loop-idx Set index to -1 |
| 5401 | * EVAL expr Push result of "expr" |
| 5402 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5403 | * - if beyond end, jump to "end" |
| 5404 | * - otherwise get item from list and push it |
| 5405 | * STORE var Store item in "var" |
| 5406 | * ... body ... |
| 5407 | * JUMP top Jump back to repeat |
| 5408 | * end: DROP Drop the result of "expr" |
| 5409 | * |
| 5410 | */ |
| 5411 | static char_u * |
| 5412 | compile_for(char_u *arg, cctx_T *cctx) |
| 5413 | { |
| 5414 | char_u *p; |
| 5415 | size_t varlen; |
| 5416 | garray_T *instr = &cctx->ctx_instr; |
| 5417 | garray_T *stack = &cctx->ctx_type_stack; |
| 5418 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5419 | lvar_T *loop_lvar; // loop iteration variable |
| 5420 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5421 | type_T *vartype; |
| 5422 | |
| 5423 | // TODO: list of variables: "for [key, value] in dict" |
| 5424 | // parse "var" |
| 5425 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5426 | ; |
| 5427 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5428 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5429 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5430 | { |
| 5431 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5432 | return NULL; |
| 5433 | } |
| 5434 | |
| 5435 | // consume "in" |
| 5436 | p = skipwhite(p); |
| 5437 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5438 | { |
| 5439 | emsg(_(e_missing_in)); |
| 5440 | return NULL; |
| 5441 | } |
| 5442 | p = skipwhite(p + 2); |
| 5443 | |
| 5444 | |
| 5445 | scope = new_scope(cctx, FOR_SCOPE); |
| 5446 | if (scope == NULL) |
| 5447 | return NULL; |
| 5448 | |
| 5449 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5450 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5451 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5452 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5453 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5454 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5455 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5456 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5457 | |
| 5458 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5459 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5460 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5461 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5462 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5463 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5464 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5465 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5466 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5467 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5468 | |
| 5469 | // compile "expr", it remains on the stack until "endfor" |
| 5470 | arg = p; |
| 5471 | if (compile_expr1(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5472 | { |
| 5473 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5474 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5475 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5476 | |
| 5477 | // now we know the type of "var" |
| 5478 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5479 | if (vartype->tt_type != VAR_LIST) |
| 5480 | { |
| 5481 | emsg(_("E1024: need a List to iterate over")); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5482 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5483 | return NULL; |
| 5484 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5485 | if (vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5486 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5487 | |
| 5488 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5489 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5490 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5491 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5492 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5493 | |
| 5494 | return arg; |
| 5495 | } |
| 5496 | |
| 5497 | /* |
| 5498 | * compile "endfor" |
| 5499 | */ |
| 5500 | static char_u * |
| 5501 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5502 | { |
| 5503 | garray_T *instr = &cctx->ctx_instr; |
| 5504 | scope_T *scope = cctx->ctx_scope; |
| 5505 | forscope_T *forscope; |
| 5506 | isn_T *isn; |
| 5507 | |
| 5508 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5509 | { |
| 5510 | emsg(_(e_for)); |
| 5511 | return NULL; |
| 5512 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5513 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5514 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5515 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5516 | |
| 5517 | // At end of ":for" scope jump back to the FOR instruction. |
| 5518 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5519 | |
| 5520 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5521 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5522 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5523 | |
| 5524 | // Fill in the "end" label any BREAK statements |
| 5525 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5526 | |
| 5527 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5528 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5529 | return NULL; |
| 5530 | |
| 5531 | vim_free(scope); |
| 5532 | |
| 5533 | return arg; |
| 5534 | } |
| 5535 | |
| 5536 | /* |
| 5537 | * compile "while expr" |
| 5538 | * |
| 5539 | * Produces instructions: |
| 5540 | * top: EVAL expr Push result of "expr" |
| 5541 | * JUMP_IF_FALSE end jump if false |
| 5542 | * ... body ... |
| 5543 | * JUMP top Jump back to repeat |
| 5544 | * end: |
| 5545 | * |
| 5546 | */ |
| 5547 | static char_u * |
| 5548 | compile_while(char_u *arg, cctx_T *cctx) |
| 5549 | { |
| 5550 | char_u *p = arg; |
| 5551 | garray_T *instr = &cctx->ctx_instr; |
| 5552 | scope_T *scope; |
| 5553 | |
| 5554 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5555 | if (scope == NULL) |
| 5556 | return NULL; |
| 5557 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5558 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5559 | |
| 5560 | // compile "expr" |
| 5561 | if (compile_expr1(&p, cctx) == FAIL) |
| 5562 | return NULL; |
| 5563 | |
| 5564 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5565 | 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] | 5566 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5567 | return FAIL; |
| 5568 | |
| 5569 | return p; |
| 5570 | } |
| 5571 | |
| 5572 | /* |
| 5573 | * compile "endwhile" |
| 5574 | */ |
| 5575 | static char_u * |
| 5576 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5577 | { |
| 5578 | scope_T *scope = cctx->ctx_scope; |
| 5579 | |
| 5580 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5581 | { |
| 5582 | emsg(_(e_while)); |
| 5583 | return NULL; |
| 5584 | } |
| 5585 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5586 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5587 | |
| 5588 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5589 | 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] | 5590 | |
| 5591 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5592 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5593 | 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] | 5594 | |
| 5595 | vim_free(scope); |
| 5596 | |
| 5597 | return arg; |
| 5598 | } |
| 5599 | |
| 5600 | /* |
| 5601 | * compile "continue" |
| 5602 | */ |
| 5603 | static char_u * |
| 5604 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5605 | { |
| 5606 | scope_T *scope = cctx->ctx_scope; |
| 5607 | |
| 5608 | for (;;) |
| 5609 | { |
| 5610 | if (scope == NULL) |
| 5611 | { |
| 5612 | emsg(_(e_continue)); |
| 5613 | return NULL; |
| 5614 | } |
| 5615 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5616 | break; |
| 5617 | scope = scope->se_outer; |
| 5618 | } |
| 5619 | |
| 5620 | // Jump back to the FOR or WHILE instruction. |
| 5621 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5622 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5623 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5624 | return arg; |
| 5625 | } |
| 5626 | |
| 5627 | /* |
| 5628 | * compile "break" |
| 5629 | */ |
| 5630 | static char_u * |
| 5631 | compile_break(char_u *arg, cctx_T *cctx) |
| 5632 | { |
| 5633 | scope_T *scope = cctx->ctx_scope; |
| 5634 | endlabel_T **el; |
| 5635 | |
| 5636 | for (;;) |
| 5637 | { |
| 5638 | if (scope == NULL) |
| 5639 | { |
| 5640 | emsg(_(e_break)); |
| 5641 | return NULL; |
| 5642 | } |
| 5643 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5644 | break; |
| 5645 | scope = scope->se_outer; |
| 5646 | } |
| 5647 | |
| 5648 | // Jump to the end of the FOR or WHILE loop. |
| 5649 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5650 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5651 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5652 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5653 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5654 | return FAIL; |
| 5655 | |
| 5656 | return arg; |
| 5657 | } |
| 5658 | |
| 5659 | /* |
| 5660 | * compile "{" start of block |
| 5661 | */ |
| 5662 | static char_u * |
| 5663 | compile_block(char_u *arg, cctx_T *cctx) |
| 5664 | { |
| 5665 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5666 | return NULL; |
| 5667 | return skipwhite(arg + 1); |
| 5668 | } |
| 5669 | |
| 5670 | /* |
| 5671 | * compile end of block: drop one scope |
| 5672 | */ |
| 5673 | static void |
| 5674 | compile_endblock(cctx_T *cctx) |
| 5675 | { |
| 5676 | scope_T *scope = cctx->ctx_scope; |
| 5677 | |
| 5678 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5679 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5680 | vim_free(scope); |
| 5681 | } |
| 5682 | |
| 5683 | /* |
| 5684 | * compile "try" |
| 5685 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5686 | * finally. |
| 5687 | * Creates another scope for the "try" block itself. |
| 5688 | * TRY instruction sets up exception handling at runtime. |
| 5689 | * |
| 5690 | * "try" |
| 5691 | * TRY -> catch1, -> finally push trystack entry |
| 5692 | * ... try block |
| 5693 | * "throw {exception}" |
| 5694 | * EVAL {exception} |
| 5695 | * THROW create exception |
| 5696 | * ... try block |
| 5697 | * " catch {expr}" |
| 5698 | * JUMP -> finally |
| 5699 | * catch1: PUSH exeception |
| 5700 | * EVAL {expr} |
| 5701 | * MATCH |
| 5702 | * JUMP nomatch -> catch2 |
| 5703 | * CATCH remove exception |
| 5704 | * ... catch block |
| 5705 | * " catch" |
| 5706 | * JUMP -> finally |
| 5707 | * catch2: CATCH remove exception |
| 5708 | * ... catch block |
| 5709 | * " finally" |
| 5710 | * finally: |
| 5711 | * ... finally block |
| 5712 | * " endtry" |
| 5713 | * ENDTRY pop trystack entry, may rethrow |
| 5714 | */ |
| 5715 | static char_u * |
| 5716 | compile_try(char_u *arg, cctx_T *cctx) |
| 5717 | { |
| 5718 | garray_T *instr = &cctx->ctx_instr; |
| 5719 | scope_T *try_scope; |
| 5720 | scope_T *scope; |
| 5721 | |
| 5722 | // scope that holds the jumps that go to catch/finally/endtry |
| 5723 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5724 | if (try_scope == NULL) |
| 5725 | return NULL; |
| 5726 | |
| 5727 | // "catch" is set when the first ":catch" is found. |
| 5728 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5729 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5730 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5731 | return NULL; |
| 5732 | |
| 5733 | // scope for the try block itself |
| 5734 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5735 | if (scope == NULL) |
| 5736 | return NULL; |
| 5737 | |
| 5738 | return arg; |
| 5739 | } |
| 5740 | |
| 5741 | /* |
| 5742 | * compile "catch {expr}" |
| 5743 | */ |
| 5744 | static char_u * |
| 5745 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5746 | { |
| 5747 | scope_T *scope = cctx->ctx_scope; |
| 5748 | garray_T *instr = &cctx->ctx_instr; |
| 5749 | char_u *p; |
| 5750 | isn_T *isn; |
| 5751 | |
| 5752 | // end block scope from :try or :catch |
| 5753 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5754 | compile_endblock(cctx); |
| 5755 | scope = cctx->ctx_scope; |
| 5756 | |
| 5757 | // Error if not in a :try scope |
| 5758 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5759 | { |
| 5760 | emsg(_(e_catch)); |
| 5761 | return NULL; |
| 5762 | } |
| 5763 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5764 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5765 | { |
| 5766 | emsg(_("E1033: catch unreachable after catch-all")); |
| 5767 | return NULL; |
| 5768 | } |
| 5769 | |
| 5770 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5771 | 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] | 5772 | JUMP_ALWAYS, cctx) == FAIL) |
| 5773 | return NULL; |
| 5774 | |
| 5775 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5776 | 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] | 5777 | if (isn->isn_arg.try.try_catch == 0) |
| 5778 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5779 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5780 | { |
| 5781 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5782 | 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] | 5783 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5784 | } |
| 5785 | |
| 5786 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 5787 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5788 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5789 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 5790 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5791 | } |
| 5792 | else |
| 5793 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5794 | char_u *end; |
| 5795 | char_u *pat; |
| 5796 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5797 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5798 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5799 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5800 | // Push v:exception, push {expr} and MATCH |
| 5801 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 5802 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5803 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5804 | if (*end != *p) |
| 5805 | { |
| 5806 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 5807 | vim_free(tofree); |
| 5808 | return FAIL; |
| 5809 | } |
| 5810 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5811 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5812 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5813 | len = (int)(end - tofree); |
| 5814 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5815 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5816 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5817 | if (pat == NULL) |
| 5818 | return FAIL; |
| 5819 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 5820 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5821 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5822 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 5823 | return NULL; |
| 5824 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5825 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5826 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 5827 | return NULL; |
| 5828 | } |
| 5829 | |
| 5830 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 5831 | return NULL; |
| 5832 | |
| 5833 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5834 | return NULL; |
| 5835 | return p; |
| 5836 | } |
| 5837 | |
| 5838 | static char_u * |
| 5839 | compile_finally(char_u *arg, cctx_T *cctx) |
| 5840 | { |
| 5841 | scope_T *scope = cctx->ctx_scope; |
| 5842 | garray_T *instr = &cctx->ctx_instr; |
| 5843 | isn_T *isn; |
| 5844 | |
| 5845 | // end block scope from :try or :catch |
| 5846 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5847 | compile_endblock(cctx); |
| 5848 | scope = cctx->ctx_scope; |
| 5849 | |
| 5850 | // Error if not in a :try scope |
| 5851 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5852 | { |
| 5853 | emsg(_(e_finally)); |
| 5854 | return NULL; |
| 5855 | } |
| 5856 | |
| 5857 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5858 | 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] | 5859 | if (isn->isn_arg.try.try_finally != 0) |
| 5860 | { |
| 5861 | emsg(_(e_finally_dup)); |
| 5862 | return NULL; |
| 5863 | } |
| 5864 | |
| 5865 | // 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] | 5866 | 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] | 5867 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 5868 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5869 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5870 | { |
| 5871 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5872 | 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] | 5873 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5874 | } |
| 5875 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5876 | // TODO: set index in ts_finally_label jumps |
| 5877 | |
| 5878 | return arg; |
| 5879 | } |
| 5880 | |
| 5881 | static char_u * |
| 5882 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 5883 | { |
| 5884 | scope_T *scope = cctx->ctx_scope; |
| 5885 | garray_T *instr = &cctx->ctx_instr; |
| 5886 | isn_T *isn; |
| 5887 | |
| 5888 | // end block scope from :catch or :finally |
| 5889 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5890 | compile_endblock(cctx); |
| 5891 | scope = cctx->ctx_scope; |
| 5892 | |
| 5893 | // Error if not in a :try scope |
| 5894 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5895 | { |
| 5896 | if (scope == NULL) |
| 5897 | emsg(_(e_no_endtry)); |
| 5898 | else if (scope->se_type == WHILE_SCOPE) |
| 5899 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 5900 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5901 | emsg(_(e_endfor)); |
| 5902 | else |
| 5903 | emsg(_(e_endif)); |
| 5904 | return NULL; |
| 5905 | } |
| 5906 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5907 | 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] | 5908 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 5909 | { |
| 5910 | emsg(_("E1032: missing :catch or :finally")); |
| 5911 | return NULL; |
| 5912 | } |
| 5913 | |
| 5914 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 5915 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5916 | 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] | 5917 | |
| 5918 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 5919 | if (isn->isn_arg.try.try_finally == 0) |
| 5920 | isn->isn_arg.try.try_finally = instr->ga_len; |
| 5921 | compile_endblock(cctx); |
| 5922 | |
| 5923 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 5924 | return NULL; |
| 5925 | return arg; |
| 5926 | } |
| 5927 | |
| 5928 | /* |
| 5929 | * compile "throw {expr}" |
| 5930 | */ |
| 5931 | static char_u * |
| 5932 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 5933 | { |
| 5934 | char_u *p = skipwhite(arg); |
| 5935 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5936 | if (compile_expr1(&p, cctx) == FAIL) |
| 5937 | return NULL; |
| 5938 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 5939 | return NULL; |
| 5940 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 5941 | return NULL; |
| 5942 | |
| 5943 | return p; |
| 5944 | } |
| 5945 | |
| 5946 | /* |
| 5947 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 5948 | * compile "echomsg expr" |
| 5949 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 5950 | * compile "execute expr" |
| 5951 | */ |
| 5952 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 5953 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 5954 | { |
| 5955 | char_u *p = arg; |
| 5956 | int count = 0; |
| 5957 | |
| 5958 | for (;;) |
| 5959 | { |
| 5960 | if (compile_expr1(&p, cctx) == FAIL) |
| 5961 | return NULL; |
| 5962 | ++count; |
| 5963 | p = skipwhite(p); |
| 5964 | if (ends_excmd(*p)) |
| 5965 | break; |
| 5966 | } |
| 5967 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 5968 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 5969 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 5970 | else if (cmdidx == CMD_execute) |
| 5971 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 5972 | else if (cmdidx == CMD_echomsg) |
| 5973 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 5974 | else |
| 5975 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5976 | return p; |
| 5977 | } |
| 5978 | |
| 5979 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 5980 | * A command that is not compiled, execute with legacy code. |
| 5981 | */ |
| 5982 | static char_u * |
| 5983 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 5984 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 5985 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 5986 | int has_expr = FALSE; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 5987 | |
| 5988 | if (cctx->ctx_skip == TRUE) |
| 5989 | goto theend; |
| 5990 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 5991 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
| 5992 | has_expr = (excmd_get_argt(eap->cmdidx) & (EX_XFILE | EX_EXPAND)); |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 5993 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 5994 | { |
| 5995 | // expand filename in "syntax include [@group] filename" |
| 5996 | has_expr = TRUE; |
| 5997 | eap->arg = skipwhite(eap->arg + 7); |
| 5998 | if (*eap->arg == '@') |
| 5999 | eap->arg = skiptowhite(eap->arg); |
| 6000 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6001 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6002 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6003 | { |
| 6004 | int count = 0; |
| 6005 | char_u *start = skipwhite(line); |
| 6006 | |
| 6007 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6008 | // PUSHS ":cmd xxx" |
| 6009 | // eval expr1 |
| 6010 | // PUSHS "yyy" |
| 6011 | // eval expr2 |
| 6012 | // PUSHS "zzz" |
| 6013 | // EXECCONCAT 5 |
| 6014 | for (;;) |
| 6015 | { |
| 6016 | if (p > start) |
| 6017 | { |
| 6018 | generate_PUSHS(cctx, vim_strnsave(start, (int)(p - start))); |
| 6019 | ++count; |
| 6020 | } |
| 6021 | p += 2; |
| 6022 | if (compile_expr1(&p, cctx) == FAIL) |
| 6023 | return NULL; |
| 6024 | may_generate_2STRING(-1, cctx); |
| 6025 | ++count; |
| 6026 | p = skipwhite(p); |
| 6027 | if (*p != '`') |
| 6028 | { |
| 6029 | emsg(_("E1083: missing backtick")); |
| 6030 | return NULL; |
| 6031 | } |
| 6032 | start = p + 1; |
| 6033 | |
| 6034 | p = (char_u *)strstr((char *)start, "`="); |
| 6035 | if (p == NULL) |
| 6036 | { |
| 6037 | if (*skipwhite(start) != NUL) |
| 6038 | { |
| 6039 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6040 | ++count; |
| 6041 | } |
| 6042 | break; |
| 6043 | } |
| 6044 | } |
| 6045 | generate_EXECCONCAT(cctx, count); |
| 6046 | } |
| 6047 | else |
| 6048 | generate_EXEC(cctx, line); |
| 6049 | |
| 6050 | theend: |
| 6051 | return (char_u *)""; |
| 6052 | } |
| 6053 | |
| 6054 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6055 | * After ex_function() has collected all the function lines: parse and compile |
| 6056 | * the lines into instructions. |
| 6057 | * Adds the function to "def_functions". |
| 6058 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6059 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6060 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6061 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6062 | * "def_functions". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6063 | */ |
| 6064 | void |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6065 | 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] | 6066 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6067 | char_u *line = NULL; |
| 6068 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6069 | char *errormsg = NULL; // error message |
| 6070 | int had_return = FALSE; |
| 6071 | cctx_T cctx; |
| 6072 | garray_T *instr; |
| 6073 | int called_emsg_before = called_emsg; |
| 6074 | int ret = FAIL; |
| 6075 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6076 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6077 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6078 | { |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6079 | dfunc_T *dfunc; // may be invalidated by compile_lambda() |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6080 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6081 | if (ufunc->uf_dfunc_idx >= 0) |
| 6082 | { |
| 6083 | // Redefining a function that was compiled before. |
| 6084 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
| 6085 | |
| 6086 | // Free old instructions. |
| 6087 | delete_def_function_contents(dfunc); |
| 6088 | } |
| 6089 | else |
| 6090 | { |
| 6091 | // Add the function to "def_functions". |
| 6092 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6093 | return; |
| 6094 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6095 | CLEAR_POINTER(dfunc); |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6096 | dfunc->df_idx = def_functions.ga_len; |
| 6097 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6098 | dfunc->df_ufunc = ufunc; |
| 6099 | ++def_functions.ga_len; |
| 6100 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6101 | } |
| 6102 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6103 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6104 | cctx.ctx_ufunc = ufunc; |
| 6105 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6106 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6107 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6108 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6109 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6110 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6111 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6112 | instr = &cctx.ctx_instr; |
| 6113 | |
| 6114 | // Most modern script version. |
| 6115 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6116 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6117 | if (ufunc->uf_def_args.ga_len > 0) |
| 6118 | { |
| 6119 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6120 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6121 | int i; |
| 6122 | char_u *arg; |
| 6123 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6124 | |
| 6125 | // Produce instructions for the default values of optional arguments. |
| 6126 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6127 | // where to start when the function is called, depending on the number |
| 6128 | // of arguments. |
| 6129 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6130 | if (ufunc->uf_def_arg_idx == NULL) |
| 6131 | goto erret; |
| 6132 | for (i = 0; i < count; ++i) |
| 6133 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6134 | garray_T *stack = &cctx.ctx_type_stack; |
| 6135 | type_T *val_type; |
| 6136 | int arg_idx = first_def_arg + i; |
| 6137 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6138 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6139 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6140 | if (compile_expr1(&arg, &cctx) == FAIL) |
| 6141 | goto erret; |
| 6142 | |
| 6143 | // If no type specified use the type of the default value. |
| 6144 | // Otherwise check that the default value type matches the |
| 6145 | // specified type. |
| 6146 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6147 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6148 | ufunc->uf_arg_types[arg_idx] = val_type; |
| 6149 | else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE) |
| 6150 | == FAIL) |
| 6151 | { |
| 6152 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6153 | arg_idx + 1); |
| 6154 | goto erret; |
| 6155 | } |
| 6156 | |
| 6157 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6158 | goto erret; |
| 6159 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6160 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6161 | } |
| 6162 | |
| 6163 | /* |
| 6164 | * Loop over all the lines of the function and generate instructions. |
| 6165 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6166 | for (;;) |
| 6167 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6168 | exarg_T ea; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6169 | int is_ex_command = FALSE; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6170 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6171 | // Bail out on the first error to avoid a flood of errors and report |
| 6172 | // the right line number when inside try/catch. |
| 6173 | if (emsg_before != called_emsg) |
| 6174 | goto erret; |
| 6175 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6176 | if (line != NULL && *line == '|') |
| 6177 | // the line continues after a '|' |
| 6178 | ++line; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6179 | else if (line != NULL && *line != NUL |
| 6180 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6181 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6182 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6183 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6184 | goto erret; |
| 6185 | } |
| 6186 | else |
| 6187 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6188 | line = next_line_from_context(&cctx); |
| 6189 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6190 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6191 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6192 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6193 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6194 | |
| 6195 | had_return = FALSE; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6196 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6197 | ea.cmdlinep = &line; |
| 6198 | ea.cmd = skipwhite(line); |
| 6199 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6200 | // Some things can be recognized by the first character. |
| 6201 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6202 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6203 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6204 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6205 | if (ea.cmd[1] != '{') |
| 6206 | { |
| 6207 | line = (char_u *)""; |
| 6208 | continue; |
| 6209 | } |
| 6210 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6211 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6212 | case '}': |
| 6213 | { |
| 6214 | // "}" ends a block scope |
| 6215 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6216 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6217 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6218 | if (stype == BLOCK_SCOPE) |
| 6219 | { |
| 6220 | compile_endblock(&cctx); |
| 6221 | line = ea.cmd; |
| 6222 | } |
| 6223 | else |
| 6224 | { |
| 6225 | emsg(_("E1025: using } outside of a block scope")); |
| 6226 | goto erret; |
| 6227 | } |
| 6228 | if (line != NULL) |
| 6229 | line = skipwhite(ea.cmd + 1); |
| 6230 | continue; |
| 6231 | } |
| 6232 | |
| 6233 | case '{': |
| 6234 | // "{" starts a block scope |
| 6235 | // "{'a': 1}->func() is something else |
| 6236 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6237 | { |
| 6238 | line = compile_block(ea.cmd, &cctx); |
| 6239 | continue; |
| 6240 | } |
| 6241 | break; |
| 6242 | |
| 6243 | case ':': |
| 6244 | is_ex_command = TRUE; |
| 6245 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | /* |
| 6249 | * COMMAND MODIFIERS |
| 6250 | */ |
| 6251 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6252 | { |
| 6253 | if (errormsg != NULL) |
| 6254 | goto erret; |
| 6255 | // empty line or comment |
| 6256 | line = (char_u *)""; |
| 6257 | continue; |
| 6258 | } |
| 6259 | |
| 6260 | // Skip ":call" to get to the function name. |
| 6261 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 6262 | ea.cmd = skipwhite(ea.cmd); |
| 6263 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6264 | if (!is_ex_command) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6265 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6266 | // Assuming the command starts with a variable or function name, |
| 6267 | // find what follows. Also "&opt = val", "$ENV = val" and "@r = |
| 6268 | // val". |
| 6269 | p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
| 6270 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6271 | p = to_name_end(p, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6272 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6273 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6274 | int oplen; |
| 6275 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6276 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6277 | oplen = assignment_len(skipwhite(p), &heredoc); |
| 6278 | if (oplen > 0) |
| 6279 | { |
| 6280 | // Recognize an assignment if we recognize the variable |
| 6281 | // name: |
| 6282 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6283 | // "local = expr" where "local" is a local var. |
| 6284 | // "script = expr" where "script" is a script-local var. |
| 6285 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6286 | // "&opt = expr" |
| 6287 | // "$ENV = expr" |
| 6288 | // "@r = expr" |
| 6289 | if (*ea.cmd == '&' |
| 6290 | || *ea.cmd == '$' |
| 6291 | || *ea.cmd == '@' |
| 6292 | || ((p - ea.cmd) > 2 && ea.cmd[1] == ':') |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6293 | || lookup_local(ea.cmd, p - ea.cmd, &cctx) != NULL |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6294 | || lookup_script(ea.cmd, p - ea.cmd) == OK |
| 6295 | || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL) |
| 6296 | { |
| 6297 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6298 | if (line == NULL) |
| 6299 | goto erret; |
| 6300 | continue; |
| 6301 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6302 | } |
| 6303 | } |
| 6304 | } |
| 6305 | |
| 6306 | /* |
| 6307 | * COMMAND after range |
| 6308 | */ |
| 6309 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6310 | p = find_ex_command(&ea, NULL, is_ex_command ? NULL |
| 6311 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6312 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6313 | |
| 6314 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6315 | { |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6316 | if (cctx.ctx_skip == TRUE) |
| 6317 | { |
| 6318 | line += STRLEN(line); |
| 6319 | continue; |
| 6320 | } |
| 6321 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6322 | // Expression or function call. |
| 6323 | if (ea.cmdidx == CMD_eval) |
| 6324 | { |
| 6325 | p = ea.cmd; |
| 6326 | if (compile_expr1(&p, &cctx) == FAIL) |
| 6327 | goto erret; |
| 6328 | |
| 6329 | // drop the return value |
| 6330 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6331 | line = p; |
| 6332 | continue; |
| 6333 | } |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6334 | // CMD_let cannot happen, compile_assignment() above is used |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6335 | iemsg("Command from find_ex_command() not handled"); |
| 6336 | goto erret; |
| 6337 | } |
| 6338 | |
| 6339 | p = skipwhite(p); |
| 6340 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6341 | if (cctx.ctx_skip == TRUE |
| 6342 | && ea.cmdidx != CMD_elseif |
| 6343 | && ea.cmdidx != CMD_else |
| 6344 | && ea.cmdidx != CMD_endif) |
| 6345 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6346 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6347 | continue; |
| 6348 | } |
| 6349 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6350 | switch (ea.cmdidx) |
| 6351 | { |
| 6352 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6353 | ea.arg = p; |
| 6354 | line = compile_nested_function(&ea, &cctx); |
| 6355 | break; |
| 6356 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6357 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6358 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6359 | goto erret; |
| 6360 | |
| 6361 | case CMD_return: |
| 6362 | line = compile_return(p, set_return_type, &cctx); |
| 6363 | had_return = TRUE; |
| 6364 | break; |
| 6365 | |
| 6366 | case CMD_let: |
| 6367 | case CMD_const: |
| 6368 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
| 6369 | break; |
| 6370 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6371 | case CMD_unlet: |
| 6372 | case CMD_unlockvar: |
| 6373 | case CMD_lockvar: |
| 6374 | line = compile_unletlock(p, &ea, &cctx); |
| 6375 | break; |
| 6376 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6377 | case CMD_import: |
| 6378 | line = compile_import(p, &cctx); |
| 6379 | break; |
| 6380 | |
| 6381 | case CMD_if: |
| 6382 | line = compile_if(p, &cctx); |
| 6383 | break; |
| 6384 | case CMD_elseif: |
| 6385 | line = compile_elseif(p, &cctx); |
| 6386 | break; |
| 6387 | case CMD_else: |
| 6388 | line = compile_else(p, &cctx); |
| 6389 | break; |
| 6390 | case CMD_endif: |
| 6391 | line = compile_endif(p, &cctx); |
| 6392 | break; |
| 6393 | |
| 6394 | case CMD_while: |
| 6395 | line = compile_while(p, &cctx); |
| 6396 | break; |
| 6397 | case CMD_endwhile: |
| 6398 | line = compile_endwhile(p, &cctx); |
| 6399 | break; |
| 6400 | |
| 6401 | case CMD_for: |
| 6402 | line = compile_for(p, &cctx); |
| 6403 | break; |
| 6404 | case CMD_endfor: |
| 6405 | line = compile_endfor(p, &cctx); |
| 6406 | break; |
| 6407 | case CMD_continue: |
| 6408 | line = compile_continue(p, &cctx); |
| 6409 | break; |
| 6410 | case CMD_break: |
| 6411 | line = compile_break(p, &cctx); |
| 6412 | break; |
| 6413 | |
| 6414 | case CMD_try: |
| 6415 | line = compile_try(p, &cctx); |
| 6416 | break; |
| 6417 | case CMD_catch: |
| 6418 | line = compile_catch(p, &cctx); |
| 6419 | break; |
| 6420 | case CMD_finally: |
| 6421 | line = compile_finally(p, &cctx); |
| 6422 | break; |
| 6423 | case CMD_endtry: |
| 6424 | line = compile_endtry(p, &cctx); |
| 6425 | break; |
| 6426 | case CMD_throw: |
| 6427 | line = compile_throw(p, &cctx); |
| 6428 | break; |
| 6429 | |
| 6430 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6431 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6432 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6433 | case CMD_echomsg: |
| 6434 | case CMD_echoerr: |
| 6435 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6436 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6437 | |
| 6438 | default: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6439 | // TODO: other commands with an expression argument |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6440 | // Not recognized, execute with do_cmdline_cmd(). |
| 6441 | ea.arg = p; |
| 6442 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6443 | break; |
| 6444 | } |
| 6445 | if (line == NULL) |
| 6446 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6447 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6448 | |
| 6449 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6450 | { |
| 6451 | iemsg("Type stack underflow"); |
| 6452 | goto erret; |
| 6453 | } |
| 6454 | } |
| 6455 | |
| 6456 | if (cctx.ctx_scope != NULL) |
| 6457 | { |
| 6458 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6459 | emsg(_(e_endif)); |
| 6460 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6461 | emsg(_(e_endwhile)); |
| 6462 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6463 | emsg(_(e_endfor)); |
| 6464 | else |
| 6465 | emsg(_("E1026: Missing }")); |
| 6466 | goto erret; |
| 6467 | } |
| 6468 | |
| 6469 | if (!had_return) |
| 6470 | { |
| 6471 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6472 | { |
| 6473 | emsg(_("E1027: Missing return statement")); |
| 6474 | goto erret; |
| 6475 | } |
| 6476 | |
| 6477 | // Return zero if there is no return at the end. |
| 6478 | generate_PUSHNR(&cctx, 0); |
| 6479 | generate_instr(&cctx, ISN_RETURN); |
| 6480 | } |
| 6481 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6482 | { |
| 6483 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6484 | + ufunc->uf_dfunc_idx; |
| 6485 | dfunc->df_deleted = FALSE; |
| 6486 | dfunc->df_instr = instr->ga_data; |
| 6487 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6488 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6489 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6490 | if (cctx.ctx_outer_used) |
| 6491 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6492 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6493 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6494 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6495 | int varargs = ufunc->uf_va_name != NULL; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6496 | int argcount = ufunc->uf_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6497 | |
| 6498 | // Create a type for the function, with the return type and any |
| 6499 | // argument types. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6500 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6501 | // The type is included in "tt_args". |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6502 | if (argcount > 0 || varargs) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6503 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6504 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6505 | argcount, &ufunc->uf_type_list); |
| 6506 | // Add argument types to the function type. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6507 | if (func_type_add_arg_types(ufunc->uf_func_type, |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6508 | argcount + varargs, |
| 6509 | &ufunc->uf_type_list) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6510 | { |
| 6511 | ret = FAIL; |
| 6512 | goto erret; |
| 6513 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6514 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6515 | ufunc->uf_func_type->tt_min_argcount = |
| 6516 | argcount - ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6517 | if (ufunc->uf_arg_types == NULL) |
| 6518 | { |
| 6519 | int i; |
| 6520 | |
| 6521 | // lambda does not have argument types. |
| 6522 | for (i = 0; i < argcount; ++i) |
| 6523 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6524 | } |
| 6525 | else |
| 6526 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6527 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6528 | if (varargs) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6529 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6530 | ufunc->uf_func_type->tt_args[argcount] = |
| 6531 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6532 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6533 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6534 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6535 | else |
| 6536 | // No arguments, can use a predefined type. |
| 6537 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6538 | argcount, &ufunc->uf_type_list); |
| 6539 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6540 | } |
| 6541 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6542 | ret = OK; |
| 6543 | |
| 6544 | erret: |
| 6545 | if (ret == FAIL) |
| 6546 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6547 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6548 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6549 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6550 | |
| 6551 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6552 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6553 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6554 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6555 | ufunc->uf_dfunc_idx = -1; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6556 | if (!dfunc->df_deleted) |
| 6557 | --def_functions.ga_len; |
| 6558 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6559 | while (cctx.ctx_scope != NULL) |
| 6560 | drop_scope(&cctx); |
| 6561 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6562 | // Don't execute this function body. |
| 6563 | ga_clear_strings(&ufunc->uf_lines); |
| 6564 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6565 | if (errormsg != NULL) |
| 6566 | emsg(errormsg); |
| 6567 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6568 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6569 | } |
| 6570 | |
| 6571 | current_sctx = save_current_sctx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6572 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6573 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6574 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6575 | } |
| 6576 | |
| 6577 | /* |
| 6578 | * Delete an instruction, free what it contains. |
| 6579 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6580 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6581 | delete_instr(isn_T *isn) |
| 6582 | { |
| 6583 | switch (isn->isn_type) |
| 6584 | { |
| 6585 | case ISN_EXEC: |
| 6586 | case ISN_LOADENV: |
| 6587 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6588 | case ISN_LOADB: |
| 6589 | case ISN_LOADW: |
| 6590 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6591 | case ISN_LOADOPT: |
| 6592 | case ISN_MEMBER: |
| 6593 | case ISN_PUSHEXC: |
| 6594 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6595 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6596 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6597 | case ISN_STOREB: |
| 6598 | case ISN_STOREW: |
| 6599 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6600 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6601 | vim_free(isn->isn_arg.string); |
| 6602 | break; |
| 6603 | |
| 6604 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6605 | case ISN_STORES: |
| 6606 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6607 | break; |
| 6608 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6609 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 6610 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6611 | vim_free(isn->isn_arg.unlet.ul_name); |
| 6612 | break; |
| 6613 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6614 | case ISN_STOREOPT: |
| 6615 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6616 | break; |
| 6617 | |
| 6618 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6619 | blob_unref(isn->isn_arg.blob); |
| 6620 | break; |
| 6621 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6622 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6623 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6624 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6625 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6626 | break; |
| 6627 | |
| 6628 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6629 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6630 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6631 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6632 | break; |
| 6633 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6634 | case ISN_UCALL: |
| 6635 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6636 | break; |
| 6637 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 6638 | case ISN_FUNCREF: |
| 6639 | { |
| 6640 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6641 | + isn->isn_arg.funcref.fr_func; |
| 6642 | func_ptr_unref(dfunc->df_ufunc); |
| 6643 | } |
| 6644 | break; |
| 6645 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6646 | case ISN_2BOOL: |
| 6647 | case ISN_2STRING: |
| 6648 | case ISN_ADDBLOB: |
| 6649 | case ISN_ADDLIST: |
| 6650 | case ISN_BCALL: |
| 6651 | case ISN_CATCH: |
| 6652 | case ISN_CHECKNR: |
| 6653 | case ISN_CHECKTYPE: |
| 6654 | case ISN_COMPAREANY: |
| 6655 | case ISN_COMPAREBLOB: |
| 6656 | case ISN_COMPAREBOOL: |
| 6657 | case ISN_COMPAREDICT: |
| 6658 | case ISN_COMPAREFLOAT: |
| 6659 | case ISN_COMPAREFUNC: |
| 6660 | case ISN_COMPARELIST: |
| 6661 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6662 | case ISN_COMPARESPECIAL: |
| 6663 | case ISN_COMPARESTRING: |
| 6664 | case ISN_CONCAT: |
| 6665 | case ISN_DCALL: |
| 6666 | case ISN_DROP: |
| 6667 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6668 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6669 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6670 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6671 | case ISN_EXECCONCAT: |
| 6672 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6673 | case ISN_FOR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6674 | case ISN_INDEX: |
| 6675 | case ISN_JUMP: |
| 6676 | case ISN_LOAD: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6677 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | case ISN_LOADSCRIPT: |
| 6679 | case ISN_LOADREG: |
| 6680 | case ISN_LOADV: |
| 6681 | case ISN_NEGATENR: |
| 6682 | case ISN_NEWDICT: |
| 6683 | case ISN_NEWLIST: |
| 6684 | case ISN_OPNR: |
| 6685 | case ISN_OPFLOAT: |
| 6686 | case ISN_OPANY: |
| 6687 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6688 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6689 | case ISN_PUSHF: |
| 6690 | case ISN_PUSHNR: |
| 6691 | case ISN_PUSHBOOL: |
| 6692 | case ISN_PUSHSPEC: |
| 6693 | case ISN_RETURN: |
| 6694 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 6695 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6696 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6697 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6698 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6699 | case ISN_STORESCRIPT: |
| 6700 | case ISN_THROW: |
| 6701 | case ISN_TRY: |
| 6702 | // nothing allocated |
| 6703 | break; |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6708 | * Free all instructions for "dfunc". |
| 6709 | */ |
| 6710 | static void |
| 6711 | delete_def_function_contents(dfunc_T *dfunc) |
| 6712 | { |
| 6713 | int idx; |
| 6714 | |
| 6715 | ga_clear(&dfunc->df_def_args_isn); |
| 6716 | |
| 6717 | if (dfunc->df_instr != NULL) |
| 6718 | { |
| 6719 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 6720 | delete_instr(dfunc->df_instr + idx); |
| 6721 | VIM_CLEAR(dfunc->df_instr); |
| 6722 | } |
| 6723 | |
| 6724 | dfunc->df_deleted = TRUE; |
| 6725 | } |
| 6726 | |
| 6727 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6728 | * When a user function is deleted, delete any associated def function. |
| 6729 | */ |
| 6730 | void |
| 6731 | delete_def_function(ufunc_T *ufunc) |
| 6732 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6733 | if (ufunc->uf_dfunc_idx >= 0) |
| 6734 | { |
| 6735 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6736 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6737 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6738 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6739 | } |
| 6740 | } |
| 6741 | |
| 6742 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6743 | /* |
| 6744 | * Free all functions defined with ":def". |
| 6745 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6746 | void |
| 6747 | free_def_functions(void) |
| 6748 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6749 | int idx; |
| 6750 | |
| 6751 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 6752 | { |
| 6753 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 6754 | |
| 6755 | delete_def_function_contents(dfunc); |
| 6756 | } |
| 6757 | |
| 6758 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6759 | } |
| 6760 | #endif |
| 6761 | |
| 6762 | |
| 6763 | #endif // FEAT_EVAL |