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 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 146 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 147 | |
| 148 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 149 | * Lookup variable "name" in the local scope and return it. |
| 150 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 152 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 153 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 154 | { |
| 155 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 156 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 157 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 158 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 160 | |
| 161 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 162 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 163 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 164 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 165 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 166 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | { |
| 168 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 169 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 171 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 172 | |
| 173 | // Find local in outer function scope. |
| 174 | if (cctx->ctx_outer != NULL) |
| 175 | { |
| 176 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 177 | if (lvar != NULL) |
| 178 | { |
| 179 | // TODO: are there situations we should not mark the outer scope as |
| 180 | // used? |
| 181 | cctx->ctx_outer_used = TRUE; |
| 182 | lvar->lv_from_outer = TRUE; |
| 183 | return lvar; |
| 184 | } |
| 185 | } |
| 186 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 187 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 191 | * Lookup an argument in the current function and an enclosing function. |
| 192 | * Returns the argument index in "idxp" |
| 193 | * Returns the argument type in "type" |
| 194 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 195 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 196 | */ |
| 197 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | lookup_arg( |
| 199 | char_u *name, |
| 200 | size_t len, |
| 201 | int *idxp, |
| 202 | type_T **type, |
| 203 | int *gen_load_outer, |
| 204 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 205 | { |
| 206 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 207 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 208 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 209 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 210 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 211 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 212 | { |
| 213 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 214 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 215 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 216 | { |
| 217 | if (idxp != NULL) |
| 218 | { |
| 219 | // Arguments are located above the frame pointer. One further |
| 220 | // if there is a vararg argument |
| 221 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 222 | + STACK_FRAME_SIZE) |
| 223 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 224 | |
| 225 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 226 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 227 | else |
| 228 | *type = &t_any; |
| 229 | } |
| 230 | return OK; |
| 231 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 232 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 233 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 234 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 235 | if (va_name != NULL |
| 236 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 237 | { |
| 238 | if (idxp != NULL) |
| 239 | { |
| 240 | // varargs is always the last argument |
| 241 | *idxp = -STACK_FRAME_SIZE - 1; |
| 242 | *type = cctx->ctx_ufunc->uf_va_type; |
| 243 | } |
| 244 | return OK; |
| 245 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 246 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 247 | if (cctx->ctx_outer != NULL) |
| 248 | { |
| 249 | // Lookup the name for an argument of the outer function. |
| 250 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 251 | == OK) |
| 252 | { |
| 253 | *gen_load_outer = TRUE; |
| 254 | return OK; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 262 | * Returnd TRUE if the script context is Vim9 script. |
| 263 | */ |
| 264 | static int |
| 265 | script_is_vim9() |
| 266 | { |
| 267 | return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9; |
| 268 | } |
| 269 | |
| 270 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 271 | * Lookup a variable in the current script. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 272 | * If "vim9script" is TRUE the script must be Vim9 script. Used for "var" |
| 273 | * without "s:". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 274 | * Returns OK or FAIL. |
| 275 | */ |
| 276 | static int |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 277 | lookup_script(char_u *name, size_t len, int vim9script) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 278 | { |
| 279 | int cc; |
| 280 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 281 | dictitem_T *di; |
| 282 | |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 283 | if (vim9script && !script_is_vim9()) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 284 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 285 | cc = name[len]; |
| 286 | name[len] = NUL; |
| 287 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 288 | name[len] = cc; |
| 289 | return di == NULL ? FAIL: OK; |
| 290 | } |
| 291 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 292 | /* |
| 293 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 294 | * compilation context "cctx". |
| 295 | * Return FAIL and give an error if it defined. |
| 296 | */ |
| 297 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 298 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 299 | { |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 300 | int c = p[len]; |
| 301 | |
| 302 | p[len] = NUL; |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 303 | if (lookup_script(p, len, FALSE) == OK |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 304 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 305 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 306 | || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK)) |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 307 | || find_imported(p, len, cctx) != NULL |
| 308 | || find_func_even_dead(p, FALSE, cctx) != NULL) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 309 | { |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 310 | p[len] = c; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 311 | semsg(_(e_name_already_defined_str), p); |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 312 | return FAIL; |
| 313 | } |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 314 | p[len] = c; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 315 | return OK; |
| 316 | } |
| 317 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 319 | ///////////////////////////////////////////////////////////////////// |
| 320 | // Following generate_ functions expect the caller to call ga_grow(). |
| 321 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 322 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 323 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 324 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 325 | /* |
| 326 | * Generate an instruction without arguments. |
| 327 | * Returns a pointer to the new instruction, NULL if failed. |
| 328 | */ |
| 329 | static isn_T * |
| 330 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 331 | { |
| 332 | garray_T *instr = &cctx->ctx_instr; |
| 333 | isn_T *isn; |
| 334 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 335 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 336 | if (ga_grow(instr, 1) == FAIL) |
| 337 | return NULL; |
| 338 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 339 | isn->isn_type = isn_type; |
| 340 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 341 | ++instr->ga_len; |
| 342 | |
| 343 | return isn; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Generate an instruction without arguments. |
| 348 | * "drop" will be removed from the stack. |
| 349 | * Returns a pointer to the new instruction, NULL if failed. |
| 350 | */ |
| 351 | static isn_T * |
| 352 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 353 | { |
| 354 | garray_T *stack = &cctx->ctx_type_stack; |
| 355 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 356 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 357 | stack->ga_len -= drop; |
| 358 | return generate_instr(cctx, isn_type); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 363 | */ |
| 364 | static isn_T * |
| 365 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 366 | { |
| 367 | isn_T *isn; |
| 368 | garray_T *stack = &cctx->ctx_type_stack; |
| 369 | |
| 370 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 371 | return NULL; |
| 372 | |
| 373 | if (ga_grow(stack, 1) == FAIL) |
| 374 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 375 | ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 376 | ++stack->ga_len; |
| 377 | |
| 378 | return isn; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 383 | * But only for simple types. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | */ |
| 385 | static int |
| 386 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 387 | { |
| 388 | isn_T *isn; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 389 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 390 | garray_T *stack = &cctx->ctx_type_stack; |
| 391 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 392 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 393 | switch ((*type)->tt_type) |
| 394 | { |
| 395 | // nothing to be done |
| 396 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 397 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 398 | // conversion possible |
| 399 | case VAR_SPECIAL: |
| 400 | case VAR_BOOL: |
| 401 | case VAR_NUMBER: |
| 402 | case VAR_FLOAT: |
| 403 | break; |
| 404 | |
| 405 | // conversion possible (with runtime check) |
| 406 | case VAR_ANY: |
| 407 | case VAR_UNKNOWN: |
| 408 | isntype = ISN_2STRING_ANY; |
| 409 | break; |
| 410 | |
| 411 | // conversion not possible |
| 412 | case VAR_VOID: |
| 413 | case VAR_BLOB: |
| 414 | case VAR_FUNC: |
| 415 | case VAR_PARTIAL: |
| 416 | case VAR_LIST: |
| 417 | case VAR_DICT: |
| 418 | case VAR_JOB: |
| 419 | case VAR_CHANNEL: |
| 420 | to_string_error((*type)->tt_type); |
| 421 | return FAIL; |
| 422 | } |
| 423 | |
| 424 | *type = &t_string; |
| 425 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 426 | return FAIL; |
| 427 | isn->isn_arg.number = offset; |
| 428 | |
| 429 | return OK; |
| 430 | } |
| 431 | |
| 432 | static int |
| 433 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 434 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 435 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 436 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 437 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 438 | { |
| 439 | if (*op == '+') |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 440 | emsg(_(e_wrong_argument_type_for_plus)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 441 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 442 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 443 | return FAIL; |
| 444 | } |
| 445 | return OK; |
| 446 | } |
| 447 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 448 | static int |
| 449 | generate_add_instr( |
| 450 | cctx_T *cctx, |
| 451 | vartype_T vartype, |
| 452 | type_T *type1, |
| 453 | type_T *type2) |
| 454 | { |
| 455 | isn_T *isn = generate_instr_drop(cctx, |
| 456 | vartype == VAR_NUMBER ? ISN_OPNR |
| 457 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 458 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 459 | #ifdef FEAT_FLOAT |
| 460 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 461 | #endif |
| 462 | : ISN_OPANY, 1); |
| 463 | |
| 464 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 465 | && type1->tt_type != VAR_ANY |
| 466 | && type2->tt_type != VAR_ANY |
| 467 | && check_number_or_float( |
| 468 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 469 | return FAIL; |
| 470 | |
| 471 | if (isn != NULL) |
| 472 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 473 | return isn == NULL ? FAIL : OK; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Get the type to use for an instruction for an operation on "type1" and |
| 478 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 479 | * fall back to runtime type checking. |
| 480 | */ |
| 481 | static vartype_T |
| 482 | operator_type(type_T *type1, type_T *type2) |
| 483 | { |
| 484 | if (type1->tt_type == type2->tt_type |
| 485 | && (type1->tt_type == VAR_NUMBER |
| 486 | || type1->tt_type == VAR_LIST |
| 487 | #ifdef FEAT_FLOAT |
| 488 | || type1->tt_type == VAR_FLOAT |
| 489 | #endif |
| 490 | || type1->tt_type == VAR_BLOB)) |
| 491 | return type1->tt_type; |
| 492 | return VAR_ANY; |
| 493 | } |
| 494 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 495 | /* |
| 496 | * Generate an instruction with two arguments. The instruction depends on the |
| 497 | * type of the arguments. |
| 498 | */ |
| 499 | static int |
| 500 | generate_two_op(cctx_T *cctx, char_u *op) |
| 501 | { |
| 502 | garray_T *stack = &cctx->ctx_type_stack; |
| 503 | type_T *type1; |
| 504 | type_T *type2; |
| 505 | vartype_T vartype; |
| 506 | isn_T *isn; |
| 507 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 508 | RETURN_OK_IF_SKIP(cctx); |
| 509 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 510 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 511 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 512 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 513 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 514 | |
| 515 | switch (*op) |
| 516 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 517 | case '+': |
| 518 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 519 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 520 | break; |
| 521 | |
| 522 | case '-': |
| 523 | case '*': |
| 524 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 525 | op) == FAIL) |
| 526 | return FAIL; |
| 527 | if (vartype == VAR_NUMBER) |
| 528 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 529 | #ifdef FEAT_FLOAT |
| 530 | else if (vartype == VAR_FLOAT) |
| 531 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 532 | #endif |
| 533 | else |
| 534 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 535 | if (isn != NULL) |
| 536 | isn->isn_arg.op.op_type = *op == '*' |
| 537 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 538 | break; |
| 539 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 540 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 541 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 542 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 543 | && type2->tt_type != VAR_NUMBER)) |
| 544 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 545 | emsg(_(e_percent_requires_number_arguments)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 546 | return FAIL; |
| 547 | } |
| 548 | isn = generate_instr_drop(cctx, |
| 549 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 550 | if (isn != NULL) |
| 551 | isn->isn_arg.op.op_type = EXPR_REM; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 556 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 557 | { |
| 558 | type_T *type = &t_any; |
| 559 | |
| 560 | #ifdef FEAT_FLOAT |
| 561 | // float+number and number+float results in float |
| 562 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 563 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 564 | type = &t_float; |
| 565 | #endif |
| 566 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 567 | } |
| 568 | |
| 569 | return OK; |
| 570 | } |
| 571 | |
| 572 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 573 | * Get the instruction to use for comparing "type1" with "type2" |
| 574 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 575 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 576 | static isntype_T |
| 577 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 578 | { |
| 579 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 580 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 581 | if (type1 == VAR_UNKNOWN) |
| 582 | type1 = VAR_ANY; |
| 583 | if (type2 == VAR_UNKNOWN) |
| 584 | type2 = VAR_ANY; |
| 585 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 586 | if (type1 == type2) |
| 587 | { |
| 588 | switch (type1) |
| 589 | { |
| 590 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 591 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 592 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 593 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 594 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 595 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 596 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 597 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 598 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 599 | default: isntype = ISN_COMPAREANY; break; |
| 600 | } |
| 601 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 602 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 603 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 604 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 605 | isntype = ISN_COMPAREANY; |
| 606 | |
| 607 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 608 | && (isntype == ISN_COMPAREBOOL |
| 609 | || isntype == ISN_COMPARESPECIAL |
| 610 | || isntype == ISN_COMPARENR |
| 611 | || isntype == ISN_COMPAREFLOAT)) |
| 612 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 613 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 614 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 615 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 616 | } |
| 617 | if (isntype == ISN_DROP |
| 618 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 619 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 620 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 621 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 622 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 623 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 624 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 625 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 626 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 627 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 628 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 629 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 630 | return isntype; |
| 631 | } |
| 632 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 633 | int |
| 634 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 635 | { |
| 636 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 637 | return FAIL; |
| 638 | return OK; |
| 639 | } |
| 640 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 643 | */ |
| 644 | static int |
| 645 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 646 | { |
| 647 | isntype_T isntype; |
| 648 | isn_T *isn; |
| 649 | garray_T *stack = &cctx->ctx_type_stack; |
| 650 | vartype_T type1; |
| 651 | vartype_T type2; |
| 652 | |
| 653 | RETURN_OK_IF_SKIP(cctx); |
| 654 | |
| 655 | // Get the known type of the two items on the stack. If they are matching |
| 656 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 657 | // checking. |
| 658 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 659 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 660 | isntype = get_compare_isn(exptype, type1, type2); |
| 661 | if (isntype == ISN_DROP) |
| 662 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 663 | |
| 664 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 665 | return FAIL; |
| 666 | isn->isn_arg.op.op_type = exptype; |
| 667 | isn->isn_arg.op.op_ic = ic; |
| 668 | |
| 669 | // takes two arguments, puts one bool back |
| 670 | if (stack->ga_len >= 2) |
| 671 | { |
| 672 | --stack->ga_len; |
| 673 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 674 | } |
| 675 | |
| 676 | return OK; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Generate an ISN_2BOOL instruction. |
| 681 | */ |
| 682 | static int |
| 683 | generate_2BOOL(cctx_T *cctx, int invert) |
| 684 | { |
| 685 | isn_T *isn; |
| 686 | garray_T *stack = &cctx->ctx_type_stack; |
| 687 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 688 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 689 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 690 | return FAIL; |
| 691 | isn->isn_arg.number = invert; |
| 692 | |
| 693 | // type becomes bool |
| 694 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 695 | |
| 696 | return OK; |
| 697 | } |
| 698 | |
| 699 | static int |
| 700 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 701 | { |
| 702 | isn_T *isn; |
| 703 | garray_T *stack = &cctx->ctx_type_stack; |
| 704 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 705 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 706 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 707 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 708 | // TODO: whole type, e.g. for a function also arg and return types |
| 709 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 710 | isn->isn_arg.type.ct_off = offset; |
| 711 | |
| 712 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 713 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 714 | |
| 715 | return OK; |
| 716 | } |
| 717 | |
| 718 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 719 | * Check that |
| 720 | * - "actual" is "expected" type or |
| 721 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 722 | * - return FAIL. |
| 723 | */ |
| 724 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 725 | need_type( |
| 726 | type_T *actual, |
| 727 | type_T *expected, |
| 728 | int offset, |
| 729 | cctx_T *cctx, |
| 730 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 731 | { |
| 732 | if (check_type(expected, actual, FALSE) == OK) |
| 733 | return OK; |
| 734 | if (actual->tt_type != VAR_ANY |
| 735 | && actual->tt_type != VAR_UNKNOWN |
| 736 | && !(actual->tt_type == VAR_FUNC |
| 737 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 738 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 739 | if (!silent) |
| 740 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 741 | return FAIL; |
| 742 | } |
| 743 | generate_TYPECHECK(cctx, expected, offset); |
| 744 | return OK; |
| 745 | } |
| 746 | |
| 747 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 748 | * Generate an ISN_PUSHNR instruction. |
| 749 | */ |
| 750 | static int |
| 751 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 752 | { |
| 753 | isn_T *isn; |
| 754 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 755 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 756 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 757 | return FAIL; |
| 758 | isn->isn_arg.number = number; |
| 759 | |
| 760 | return OK; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Generate an ISN_PUSHBOOL instruction. |
| 765 | */ |
| 766 | static int |
| 767 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 768 | { |
| 769 | isn_T *isn; |
| 770 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 771 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 772 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 773 | return FAIL; |
| 774 | isn->isn_arg.number = number; |
| 775 | |
| 776 | return OK; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Generate an ISN_PUSHSPEC instruction. |
| 781 | */ |
| 782 | static int |
| 783 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 784 | { |
| 785 | isn_T *isn; |
| 786 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 787 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 788 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 789 | return FAIL; |
| 790 | isn->isn_arg.number = number; |
| 791 | |
| 792 | return OK; |
| 793 | } |
| 794 | |
| 795 | #ifdef FEAT_FLOAT |
| 796 | /* |
| 797 | * Generate an ISN_PUSHF instruction. |
| 798 | */ |
| 799 | static int |
| 800 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 801 | { |
| 802 | isn_T *isn; |
| 803 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 804 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 805 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 806 | return FAIL; |
| 807 | isn->isn_arg.fnumber = fnumber; |
| 808 | |
| 809 | return OK; |
| 810 | } |
| 811 | #endif |
| 812 | |
| 813 | /* |
| 814 | * Generate an ISN_PUSHS instruction. |
| 815 | * Consumes "str". |
| 816 | */ |
| 817 | static int |
| 818 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 819 | { |
| 820 | isn_T *isn; |
| 821 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 822 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 823 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 824 | return FAIL; |
| 825 | isn->isn_arg.string = str; |
| 826 | |
| 827 | return OK; |
| 828 | } |
| 829 | |
| 830 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 831 | * Generate an ISN_PUSHCHANNEL instruction. |
| 832 | * Consumes "channel". |
| 833 | */ |
| 834 | static int |
| 835 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 836 | { |
| 837 | isn_T *isn; |
| 838 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 839 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 840 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 841 | return FAIL; |
| 842 | isn->isn_arg.channel = channel; |
| 843 | |
| 844 | return OK; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Generate an ISN_PUSHJOB instruction. |
| 849 | * Consumes "job". |
| 850 | */ |
| 851 | static int |
| 852 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 853 | { |
| 854 | isn_T *isn; |
| 855 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 856 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 857 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 858 | return FAIL; |
| 859 | isn->isn_arg.job = job; |
| 860 | |
| 861 | return OK; |
| 862 | } |
| 863 | |
| 864 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 865 | * Generate an ISN_PUSHBLOB instruction. |
| 866 | * Consumes "blob". |
| 867 | */ |
| 868 | static int |
| 869 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 870 | { |
| 871 | isn_T *isn; |
| 872 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 873 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 875 | return FAIL; |
| 876 | isn->isn_arg.blob = blob; |
| 877 | |
| 878 | return OK; |
| 879 | } |
| 880 | |
| 881 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 882 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 883 | * Consumes "name". |
| 884 | */ |
| 885 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 886 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 887 | { |
| 888 | isn_T *isn; |
| 889 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 890 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 891 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 892 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 893 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 894 | |
| 895 | return OK; |
| 896 | } |
| 897 | |
| 898 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 899 | * Generate an ISN_GETITEM instruction with "index". |
| 900 | */ |
| 901 | static int |
| 902 | generate_GETITEM(cctx_T *cctx, int index) |
| 903 | { |
| 904 | isn_T *isn; |
| 905 | garray_T *stack = &cctx->ctx_type_stack; |
| 906 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 907 | type_T *item_type = &t_any; |
| 908 | |
| 909 | RETURN_OK_IF_SKIP(cctx); |
| 910 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 911 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 912 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 913 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 914 | emsg(_(e_listreq)); |
| 915 | return FAIL; |
| 916 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 917 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 918 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 919 | return FAIL; |
| 920 | isn->isn_arg.number = index; |
| 921 | |
| 922 | // add the item type to the type stack |
| 923 | if (ga_grow(stack, 1) == FAIL) |
| 924 | return FAIL; |
| 925 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 926 | ++stack->ga_len; |
| 927 | return OK; |
| 928 | } |
| 929 | |
| 930 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 931 | * Generate an ISN_SLICE instruction with "count". |
| 932 | */ |
| 933 | static int |
| 934 | generate_SLICE(cctx_T *cctx, int count) |
| 935 | { |
| 936 | isn_T *isn; |
| 937 | |
| 938 | RETURN_OK_IF_SKIP(cctx); |
| 939 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 940 | return FAIL; |
| 941 | isn->isn_arg.number = count; |
| 942 | return OK; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 947 | */ |
| 948 | static int |
| 949 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 950 | { |
| 951 | isn_T *isn; |
| 952 | |
| 953 | RETURN_OK_IF_SKIP(cctx); |
| 954 | |
| 955 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 956 | return FAIL; |
| 957 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 958 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 959 | |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 964 | * Generate an ISN_STORE instruction. |
| 965 | */ |
| 966 | static int |
| 967 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 968 | { |
| 969 | isn_T *isn; |
| 970 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 971 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 972 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 973 | return FAIL; |
| 974 | if (name != NULL) |
| 975 | isn->isn_arg.string = vim_strsave(name); |
| 976 | else |
| 977 | isn->isn_arg.number = idx; |
| 978 | |
| 979 | return OK; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 984 | */ |
| 985 | static int |
| 986 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 987 | { |
| 988 | isn_T *isn; |
| 989 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 990 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 991 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 992 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 993 | isn->isn_arg.storenr.stnr_idx = idx; |
| 994 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 995 | |
| 996 | return OK; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * Generate an ISN_STOREOPT instruction |
| 1001 | */ |
| 1002 | static int |
| 1003 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1004 | { |
| 1005 | isn_T *isn; |
| 1006 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1007 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1008 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1009 | return FAIL; |
| 1010 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1011 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1012 | |
| 1013 | return OK; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Generate an ISN_LOAD or similar instruction. |
| 1018 | */ |
| 1019 | static int |
| 1020 | generate_LOAD( |
| 1021 | cctx_T *cctx, |
| 1022 | isntype_T isn_type, |
| 1023 | int idx, |
| 1024 | char_u *name, |
| 1025 | type_T *type) |
| 1026 | { |
| 1027 | isn_T *isn; |
| 1028 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1029 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1030 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1031 | return FAIL; |
| 1032 | if (name != NULL) |
| 1033 | isn->isn_arg.string = vim_strsave(name); |
| 1034 | else |
| 1035 | isn->isn_arg.number = idx; |
| 1036 | |
| 1037 | return OK; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1041 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1042 | */ |
| 1043 | static int |
| 1044 | generate_LOADV( |
| 1045 | cctx_T *cctx, |
| 1046 | char_u *name, |
| 1047 | int error) |
| 1048 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1049 | int di_flags; |
| 1050 | int vidx = find_vim_var(name, &di_flags); |
| 1051 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1052 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1053 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1054 | if (vidx < 0) |
| 1055 | { |
| 1056 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1057 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1058 | return FAIL; |
| 1059 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1060 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1061 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1062 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1066 | * Generate an ISN_UNLET instruction. |
| 1067 | */ |
| 1068 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1069 | 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] | 1070 | { |
| 1071 | isn_T *isn; |
| 1072 | |
| 1073 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1074 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1075 | return FAIL; |
| 1076 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1077 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1078 | |
| 1079 | return OK; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1083 | * Generate an ISN_LOADS instruction. |
| 1084 | */ |
| 1085 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1086 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1087 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1088 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1089 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1090 | int sid, |
| 1091 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1092 | { |
| 1093 | isn_T *isn; |
| 1094 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1095 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1096 | if (isn_type == ISN_LOADS) |
| 1097 | isn = generate_instr_type(cctx, isn_type, type); |
| 1098 | else |
| 1099 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1100 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1101 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1102 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1103 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1104 | |
| 1105 | return OK; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1110 | */ |
| 1111 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1112 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1113 | cctx_T *cctx, |
| 1114 | isntype_T isn_type, |
| 1115 | int sid, |
| 1116 | int idx, |
| 1117 | type_T *type) |
| 1118 | { |
| 1119 | isn_T *isn; |
| 1120 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1121 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1122 | if (isn_type == ISN_LOADSCRIPT) |
| 1123 | isn = generate_instr_type(cctx, isn_type, type); |
| 1124 | else |
| 1125 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1126 | if (isn == NULL) |
| 1127 | return FAIL; |
| 1128 | isn->isn_arg.script.script_sid = sid; |
| 1129 | isn->isn_arg.script.script_idx = idx; |
| 1130 | return OK; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Generate an ISN_NEWLIST instruction. |
| 1135 | */ |
| 1136 | static int |
| 1137 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1138 | { |
| 1139 | isn_T *isn; |
| 1140 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1141 | type_T *type; |
| 1142 | type_T *member; |
| 1143 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1144 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1145 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1146 | return FAIL; |
| 1147 | isn->isn_arg.number = count; |
| 1148 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1149 | // get the member type from all the items on the stack. |
| 1150 | member = get_member_type_from_stack( |
| 1151 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1152 | cctx->ctx_type_list); |
| 1153 | type = get_list_type(member, cctx->ctx_type_list); |
| 1154 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1155 | // drop the value types |
| 1156 | stack->ga_len -= count; |
| 1157 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1158 | // add the list type to the type stack |
| 1159 | if (ga_grow(stack, 1) == FAIL) |
| 1160 | return FAIL; |
| 1161 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1162 | ++stack->ga_len; |
| 1163 | |
| 1164 | return OK; |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * Generate an ISN_NEWDICT instruction. |
| 1169 | */ |
| 1170 | static int |
| 1171 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1172 | { |
| 1173 | isn_T *isn; |
| 1174 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1175 | type_T *type; |
| 1176 | type_T *member; |
| 1177 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1178 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1179 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1180 | return FAIL; |
| 1181 | isn->isn_arg.number = count; |
| 1182 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1183 | member = get_member_type_from_stack( |
| 1184 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1185 | cctx->ctx_type_list); |
| 1186 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1187 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1188 | // drop the key and value types |
| 1189 | stack->ga_len -= 2 * count; |
| 1190 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1191 | // add the dict type to the type stack |
| 1192 | if (ga_grow(stack, 1) == FAIL) |
| 1193 | return FAIL; |
| 1194 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1195 | ++stack->ga_len; |
| 1196 | |
| 1197 | return OK; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * Generate an ISN_FUNCREF instruction. |
| 1202 | */ |
| 1203 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1204 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1205 | { |
| 1206 | isn_T *isn; |
| 1207 | garray_T *stack = &cctx->ctx_type_stack; |
| 1208 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1209 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1210 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1211 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1212 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1213 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1214 | |
| 1215 | if (ga_grow(stack, 1) == FAIL) |
| 1216 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1217 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1218 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1219 | ++stack->ga_len; |
| 1220 | |
| 1221 | return OK; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1225 | * Generate an ISN_NEWFUNC instruction. |
| 1226 | */ |
| 1227 | static int |
| 1228 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1229 | { |
| 1230 | isn_T *isn; |
| 1231 | char_u *name; |
| 1232 | |
| 1233 | RETURN_OK_IF_SKIP(cctx); |
| 1234 | name = vim_strsave(lambda_name); |
| 1235 | if (name == NULL) |
| 1236 | return FAIL; |
| 1237 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1238 | return FAIL; |
| 1239 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1240 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1241 | |
| 1242 | return OK; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1246 | * Generate an ISN_JUMP instruction. |
| 1247 | */ |
| 1248 | static int |
| 1249 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1250 | { |
| 1251 | isn_T *isn; |
| 1252 | garray_T *stack = &cctx->ctx_type_stack; |
| 1253 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1254 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1255 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1256 | return FAIL; |
| 1257 | isn->isn_arg.jump.jump_when = when; |
| 1258 | isn->isn_arg.jump.jump_where = where; |
| 1259 | |
| 1260 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1261 | --stack->ga_len; |
| 1262 | |
| 1263 | return OK; |
| 1264 | } |
| 1265 | |
| 1266 | static int |
| 1267 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1268 | { |
| 1269 | isn_T *isn; |
| 1270 | garray_T *stack = &cctx->ctx_type_stack; |
| 1271 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1272 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1273 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1274 | return FAIL; |
| 1275 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1276 | |
| 1277 | if (ga_grow(stack, 1) == FAIL) |
| 1278 | return FAIL; |
| 1279 | // type doesn't matter, will be stored next |
| 1280 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1281 | ++stack->ga_len; |
| 1282 | |
| 1283 | return OK; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1288 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1289 | * Return FAIL if the number of arguments is wrong. |
| 1290 | */ |
| 1291 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1292 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1293 | { |
| 1294 | isn_T *isn; |
| 1295 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1296 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1297 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1298 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1299 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1300 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1301 | argoff = check_internal_func(func_idx, argcount); |
| 1302 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1303 | return FAIL; |
| 1304 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1305 | if (method_call && argoff > 1) |
| 1306 | { |
| 1307 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1308 | return FAIL; |
| 1309 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1310 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1311 | } |
| 1312 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1313 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1314 | return FAIL; |
| 1315 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1316 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1317 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1318 | for (i = 0; i < argcount; ++i) |
| 1319 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1320 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1321 | stack->ga_len -= argcount; // drop the arguments |
| 1322 | if (ga_grow(stack, 1) == FAIL) |
| 1323 | return FAIL; |
| 1324 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1325 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1326 | ++stack->ga_len; // add return value |
| 1327 | |
| 1328 | return OK; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1333 | * Return FAIL if the number of arguments is wrong. |
| 1334 | */ |
| 1335 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1336 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1337 | { |
| 1338 | isn_T *isn; |
| 1339 | garray_T *stack = &cctx->ctx_type_stack; |
| 1340 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1341 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1342 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1343 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1344 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1345 | { |
| 1346 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1347 | return FAIL; |
| 1348 | } |
| 1349 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1350 | { |
| 1351 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1352 | return FAIL; |
| 1353 | } |
| 1354 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1355 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1356 | { |
| 1357 | int i; |
| 1358 | |
| 1359 | for (i = 0; i < argcount; ++i) |
| 1360 | { |
| 1361 | type_T *expected; |
| 1362 | type_T *actual; |
| 1363 | |
| 1364 | if (i < regular_args) |
| 1365 | { |
| 1366 | if (ufunc->uf_arg_types == NULL) |
| 1367 | continue; |
| 1368 | expected = ufunc->uf_arg_types[i]; |
| 1369 | } |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1370 | else if (ufunc->uf_va_type == NULL) |
| 1371 | // possibly a lambda |
| 1372 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1373 | else |
| 1374 | expected = ufunc->uf_va_type->tt_member; |
| 1375 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1376 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1377 | { |
| 1378 | arg_type_mismatch(expected, actual, i + 1); |
| 1379 | return FAIL; |
| 1380 | } |
| 1381 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1382 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1383 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1384 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1385 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1388 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1389 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1390 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1391 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1392 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1393 | { |
| 1394 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1395 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1396 | } |
| 1397 | else |
| 1398 | { |
| 1399 | // A user function may be deleted and redefined later, can't use the |
| 1400 | // ufunc pointer, need to look it up again at runtime. |
| 1401 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1402 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1403 | } |
| 1404 | |
| 1405 | stack->ga_len -= argcount; // drop the arguments |
| 1406 | if (ga_grow(stack, 1) == FAIL) |
| 1407 | return FAIL; |
| 1408 | // add return value |
| 1409 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1410 | ++stack->ga_len; |
| 1411 | |
| 1412 | return OK; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1417 | */ |
| 1418 | static int |
| 1419 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1420 | { |
| 1421 | isn_T *isn; |
| 1422 | garray_T *stack = &cctx->ctx_type_stack; |
| 1423 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1424 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1425 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1426 | return FAIL; |
| 1427 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1428 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1429 | |
| 1430 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1431 | if (ga_grow(stack, 1) == FAIL) |
| 1432 | return FAIL; |
| 1433 | // add return value |
| 1434 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1435 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1436 | |
| 1437 | return OK; |
| 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1442 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1443 | */ |
| 1444 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1445 | generate_PCALL( |
| 1446 | cctx_T *cctx, |
| 1447 | int argcount, |
| 1448 | char_u *name, |
| 1449 | type_T *type, |
| 1450 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1451 | { |
| 1452 | isn_T *isn; |
| 1453 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1454 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1456 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1457 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1458 | if (type->tt_type == VAR_ANY) |
| 1459 | ret_type = &t_any; |
| 1460 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1461 | { |
| 1462 | if (type->tt_argcount != -1) |
| 1463 | { |
| 1464 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1465 | |
| 1466 | if (argcount < type->tt_min_argcount - varargs) |
| 1467 | { |
| 1468 | semsg(_(e_toofewarg), "[reference]"); |
| 1469 | return FAIL; |
| 1470 | } |
| 1471 | if (!varargs && argcount > type->tt_argcount) |
| 1472 | { |
| 1473 | semsg(_(e_toomanyarg), "[reference]"); |
| 1474 | return FAIL; |
| 1475 | } |
| 1476 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1477 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1478 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1479 | else |
| 1480 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1481 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1482 | return FAIL; |
| 1483 | } |
| 1484 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1485 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1486 | return FAIL; |
| 1487 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1488 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1489 | |
| 1490 | stack->ga_len -= argcount; // drop the arguments |
| 1491 | |
| 1492 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1493 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1494 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1495 | // If partial is above the arguments it must be cleared and replaced with |
| 1496 | // the return value. |
| 1497 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1498 | return FAIL; |
| 1499 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1500 | return OK; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1504 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1505 | */ |
| 1506 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1507 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1508 | { |
| 1509 | isn_T *isn; |
| 1510 | garray_T *stack = &cctx->ctx_type_stack; |
| 1511 | type_T *type; |
| 1512 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1513 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1514 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1515 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1516 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1517 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1518 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1519 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1520 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1521 | { |
| 1522 | emsg(_(e_dictreq)); |
| 1523 | return FAIL; |
| 1524 | } |
| 1525 | // change dict type to dict member type |
| 1526 | if (type->tt_type == VAR_DICT) |
| 1527 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1528 | |
| 1529 | return OK; |
| 1530 | } |
| 1531 | |
| 1532 | /* |
| 1533 | * Generate an ISN_ECHO instruction. |
| 1534 | */ |
| 1535 | static int |
| 1536 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1537 | { |
| 1538 | isn_T *isn; |
| 1539 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1540 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1541 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1542 | return FAIL; |
| 1543 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1544 | isn->isn_arg.echo.echo_count = count; |
| 1545 | |
| 1546 | return OK; |
| 1547 | } |
| 1548 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1549 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1550 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1551 | */ |
| 1552 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1553 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1554 | { |
| 1555 | isn_T *isn; |
| 1556 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1557 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1558 | return FAIL; |
| 1559 | isn->isn_arg.number = count; |
| 1560 | |
| 1561 | return OK; |
| 1562 | } |
| 1563 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1564 | static int |
| 1565 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1566 | { |
| 1567 | isn_T *isn; |
| 1568 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1569 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1570 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1571 | return FAIL; |
| 1572 | isn->isn_arg.string = vim_strsave(line); |
| 1573 | return OK; |
| 1574 | } |
| 1575 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1576 | static int |
| 1577 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1578 | { |
| 1579 | isn_T *isn; |
| 1580 | |
| 1581 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1582 | return FAIL; |
| 1583 | isn->isn_arg.number = count; |
| 1584 | return OK; |
| 1585 | } |
| 1586 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1587 | /* |
| 1588 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1589 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1590 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1591 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1592 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1593 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1594 | lvar_T *lvar; |
| 1595 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1596 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1597 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1598 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1599 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1603 | return NULL; |
| 1604 | 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] | 1605 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1606 | // Every local variable uses the next entry on the stack. We could re-use |
| 1607 | // the last ones when leaving a scope, but then variables used in a closure |
| 1608 | // might get overwritten. To keep things simple do not re-use stack |
| 1609 | // entries. This is less efficient, but memory is cheap these days. |
| 1610 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1611 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1612 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1613 | lvar->lv_const = isConst; |
| 1614 | lvar->lv_type = type; |
| 1615 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1616 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1620 | * Remove local variables above "new_top". |
| 1621 | */ |
| 1622 | static void |
| 1623 | unwind_locals(cctx_T *cctx, int new_top) |
| 1624 | { |
| 1625 | if (cctx->ctx_locals.ga_len > new_top) |
| 1626 | { |
| 1627 | int idx; |
| 1628 | lvar_T *lvar; |
| 1629 | |
| 1630 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1631 | { |
| 1632 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1633 | vim_free(lvar->lv_name); |
| 1634 | } |
| 1635 | } |
| 1636 | cctx->ctx_locals.ga_len = new_top; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * Free all local variables. |
| 1641 | */ |
| 1642 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1643 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1644 | { |
| 1645 | unwind_locals(cctx, 0); |
| 1646 | ga_clear(&cctx->ctx_locals); |
| 1647 | } |
| 1648 | |
| 1649 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1650 | * Find "name" in script-local items of script "sid". |
| 1651 | * Returns the index in "sn_var_vals" if found. |
| 1652 | * If found but not in "sn_var_vals" returns -1. |
| 1653 | * If not found returns -2. |
| 1654 | */ |
| 1655 | int |
| 1656 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1657 | { |
| 1658 | hashtab_T *ht; |
| 1659 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1660 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1661 | int idx; |
| 1662 | |
| 1663 | // First look the name up in the hashtable. |
| 1664 | if (sid <= 0 || sid > script_items.ga_len) |
| 1665 | return -1; |
| 1666 | ht = &SCRIPT_VARS(sid); |
| 1667 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1668 | if (di == NULL) |
| 1669 | return -2; |
| 1670 | |
| 1671 | // Now find the svar_T index in sn_var_vals. |
| 1672 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1673 | { |
| 1674 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1675 | |
| 1676 | if (sv->sv_tv == &di->di_tv) |
| 1677 | { |
| 1678 | if (check_writable && sv->sv_const) |
| 1679 | semsg(_(e_readonlyvar), name); |
| 1680 | return idx; |
| 1681 | } |
| 1682 | } |
| 1683 | return -1; |
| 1684 | } |
| 1685 | |
| 1686 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1687 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1688 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1689 | */ |
| 1690 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1691 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1692 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1693 | int idx; |
| 1694 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1695 | if (current_sctx.sc_sid <= 0) |
| 1696 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1697 | if (cctx != NULL) |
| 1698 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1699 | { |
| 1700 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1701 | + idx; |
| 1702 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1703 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1704 | : STRLEN(import->imp_name) == len |
| 1705 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1706 | return import; |
| 1707 | } |
| 1708 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1709 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1710 | } |
| 1711 | |
| 1712 | imported_T * |
| 1713 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1714 | { |
| 1715 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1716 | int idx; |
| 1717 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1718 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1719 | { |
| 1720 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1721 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1722 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1723 | : STRLEN(import->imp_name) == len |
| 1724 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1725 | return import; |
| 1726 | } |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | |
| 1730 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1731 | * Free all imported variables. |
| 1732 | */ |
| 1733 | static void |
| 1734 | free_imported(cctx_T *cctx) |
| 1735 | { |
| 1736 | int idx; |
| 1737 | |
| 1738 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1739 | { |
| 1740 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1741 | |
| 1742 | vim_free(import->imp_name); |
| 1743 | } |
| 1744 | ga_clear(&cctx->ctx_imports); |
| 1745 | } |
| 1746 | |
| 1747 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1748 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1749 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1750 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1751 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1752 | { |
| 1753 | return p[0] == '#' && p[1] != '{'; |
| 1754 | } |
| 1755 | |
| 1756 | /* |
| 1757 | * Return a pointer to the next line that isn't empty or only contains a |
| 1758 | * comment. Skips over white space. |
| 1759 | * Returns NULL if there is none. |
| 1760 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1761 | char_u * |
| 1762 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1763 | { |
| 1764 | int lnum = cctx->ctx_lnum; |
| 1765 | |
| 1766 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1767 | { |
| 1768 | char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum]; |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 1769 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1770 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 1771 | // ignore NULLs inserted for continuation lines |
| 1772 | if (line != NULL) |
| 1773 | { |
| 1774 | p = skipwhite(line); |
| 1775 | if (*p != NUL && !vim9_comment_start(p)) |
| 1776 | return p; |
| 1777 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1778 | } |
| 1779 | return NULL; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1783 | * Called when checking for a following operator at "arg". When the rest of |
| 1784 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1785 | * line return a pointer to it and set "nextp". |
| 1786 | * Otherwise skip over white space. |
| 1787 | */ |
| 1788 | static char_u * |
| 1789 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1790 | { |
| 1791 | char_u *p = skipwhite(arg); |
| 1792 | |
| 1793 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1794 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1795 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1796 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1797 | if (*nextp != NULL) |
| 1798 | return *nextp; |
| 1799 | } |
| 1800 | return p; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1804 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1805 | * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE. |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1806 | * Returns NULL when at the end. |
| 1807 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1808 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1809 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1810 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1811 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1812 | |
| 1813 | do |
| 1814 | { |
| 1815 | ++cctx->ctx_lnum; |
| 1816 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1817 | { |
| 1818 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1819 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1820 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1821 | 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] | 1822 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1823 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1824 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1825 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1826 | return line; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1830 | * 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] | 1831 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1832 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1833 | */ |
| 1834 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1835 | 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] | 1836 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1837 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1838 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1839 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1840 | |
| 1841 | if (next == NULL) |
| 1842 | return FAIL; |
| 1843 | *arg = skipwhite(next); |
| 1844 | } |
| 1845 | return OK; |
| 1846 | } |
| 1847 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1848 | /* |
| 1849 | * Idem, and give an error when failed. |
| 1850 | */ |
| 1851 | static int |
| 1852 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1853 | { |
| 1854 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1855 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1856 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1857 | return FAIL; |
| 1858 | } |
| 1859 | return OK; |
| 1860 | } |
| 1861 | |
| 1862 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1863 | // Structure passed between the compile_expr* functions to keep track of |
| 1864 | // constants that have been parsed but for which no code was produced yet. If |
| 1865 | // possible expressions on these constants are applied at compile time. If |
| 1866 | // that is not possible, the code to push the constants needs to be generated |
| 1867 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1868 | // Using 50 should be more than enough of 5 levels of (). |
| 1869 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1870 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1871 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1872 | int pp_used; // active entries in pp_tv[] |
| 1873 | } ppconst_T; |
| 1874 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1875 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1876 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1877 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1878 | /* |
| 1879 | * Generate a PUSH instruction for "tv". |
| 1880 | * "tv" will be consumed or cleared. |
| 1881 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1882 | */ |
| 1883 | static int |
| 1884 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1885 | { |
| 1886 | if (tv != NULL) |
| 1887 | { |
| 1888 | switch (tv->v_type) |
| 1889 | { |
| 1890 | case VAR_UNKNOWN: |
| 1891 | break; |
| 1892 | case VAR_BOOL: |
| 1893 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1894 | break; |
| 1895 | case VAR_SPECIAL: |
| 1896 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1897 | break; |
| 1898 | case VAR_NUMBER: |
| 1899 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1900 | break; |
| 1901 | #ifdef FEAT_FLOAT |
| 1902 | case VAR_FLOAT: |
| 1903 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1904 | break; |
| 1905 | #endif |
| 1906 | case VAR_BLOB: |
| 1907 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1908 | tv->vval.v_blob = NULL; |
| 1909 | break; |
| 1910 | case VAR_STRING: |
| 1911 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1912 | tv->vval.v_string = NULL; |
| 1913 | break; |
| 1914 | default: |
| 1915 | iemsg("constant type not supported"); |
| 1916 | clear_tv(tv); |
| 1917 | return FAIL; |
| 1918 | } |
| 1919 | tv->v_type = VAR_UNKNOWN; |
| 1920 | } |
| 1921 | return OK; |
| 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * Generate code for any ppconst entries. |
| 1926 | */ |
| 1927 | static int |
| 1928 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1929 | { |
| 1930 | int i; |
| 1931 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1932 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1933 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1934 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1935 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1936 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1937 | ret = FAIL; |
| 1938 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1939 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1940 | return ret; |
| 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * Clear ppconst constants. Used when failing. |
| 1945 | */ |
| 1946 | static void |
| 1947 | clear_ppconst(ppconst_T *ppconst) |
| 1948 | { |
| 1949 | int i; |
| 1950 | |
| 1951 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1952 | clear_tv(&ppconst->pp_tv[i]); |
| 1953 | ppconst->pp_used = 0; |
| 1954 | } |
| 1955 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1956 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1957 | * Generate an instruction to load script-local variable "name", without the |
| 1958 | * leading "s:". |
| 1959 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1960 | */ |
| 1961 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1962 | compile_load_scriptvar( |
| 1963 | cctx_T *cctx, |
| 1964 | char_u *name, // variable NUL terminated |
| 1965 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 1966 | char_u **end, // end of variable |
| 1967 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1968 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1969 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1970 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 1971 | imported_T *import; |
| 1972 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1973 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1974 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1975 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1976 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 1977 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1978 | } |
| 1979 | if (idx >= 0) |
| 1980 | { |
| 1981 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1982 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1983 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1984 | current_sctx.sc_sid, idx, sv->sv_type); |
| 1985 | return OK; |
| 1986 | } |
| 1987 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1988 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1989 | if (import != NULL) |
| 1990 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1991 | if (import->imp_all) |
| 1992 | { |
| 1993 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 1994 | char_u *exp_name; |
| 1995 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1996 | ufunc_T *ufunc; |
| 1997 | type_T *type; |
| 1998 | |
| 1999 | // Used "import * as Name", need to lookup the member. |
| 2000 | if (*p != '.') |
| 2001 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2002 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2003 | return FAIL; |
| 2004 | } |
| 2005 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2006 | if (VIM_ISWHITE(*p)) |
| 2007 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2008 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2009 | return FAIL; |
| 2010 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2011 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2012 | // isolate one name |
| 2013 | exp_name = p; |
| 2014 | while (eval_isnamec(*p)) |
| 2015 | ++p; |
| 2016 | cc = *p; |
| 2017 | *p = NUL; |
| 2018 | |
| 2019 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2020 | *p = cc; |
| 2021 | p = skipwhite(p); |
| 2022 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2023 | // TODO: what if it is a function? |
| 2024 | if (idx < 0) |
| 2025 | return FAIL; |
| 2026 | *end = p; |
| 2027 | |
| 2028 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2029 | import->imp_sid, |
| 2030 | idx, |
| 2031 | type); |
| 2032 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2033 | else if (import->imp_funcname != NULL) |
| 2034 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2035 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2036 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2037 | import->imp_sid, |
| 2038 | import->imp_var_vals_idx, |
| 2039 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2040 | return OK; |
| 2041 | } |
| 2042 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2043 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2044 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2045 | return FAIL; |
| 2046 | } |
| 2047 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2048 | static int |
| 2049 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2050 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2051 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2052 | |
| 2053 | if (ufunc == NULL) |
| 2054 | return FAIL; |
| 2055 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2056 | // Need to compile any default values to get the argument types. |
| 2057 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2058 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2059 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2060 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2063 | /* |
| 2064 | * Compile a variable name into a load instruction. |
| 2065 | * "end" points to just after the name. |
| 2066 | * When "error" is FALSE do not give an error when not found. |
| 2067 | */ |
| 2068 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2069 | 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] | 2070 | { |
| 2071 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2072 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2073 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2074 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2075 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2076 | |
| 2077 | if (*(*arg + 1) == ':') |
| 2078 | { |
| 2079 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2080 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2081 | { |
| 2082 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2083 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2084 | switch (**arg) |
| 2085 | { |
| 2086 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2087 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2088 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2089 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2090 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2091 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2092 | goto theend; |
| 2093 | } |
| 2094 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2095 | goto theend; |
| 2096 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2097 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2098 | else |
| 2099 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2100 | isntype_T isn_type = ISN_DROP; |
| 2101 | |
| 2102 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2103 | if (name == NULL) |
| 2104 | return FAIL; |
| 2105 | |
| 2106 | switch (**arg) |
| 2107 | { |
| 2108 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2109 | break; |
| 2110 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2111 | NULL, NULL, error); |
| 2112 | break; |
| 2113 | case 'g': isn_type = ISN_LOADG; break; |
| 2114 | case 'w': isn_type = ISN_LOADW; break; |
| 2115 | case 't': isn_type = ISN_LOADT; break; |
| 2116 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2117 | default: semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2118 | goto theend; |
| 2119 | } |
| 2120 | if (isn_type != ISN_DROP) |
| 2121 | { |
| 2122 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2123 | // variables can be defined later, thus we don't check if it |
| 2124 | // exists, give error at runtime. |
| 2125 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2126 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | else |
| 2130 | { |
| 2131 | size_t len = end - *arg; |
| 2132 | int idx; |
| 2133 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2134 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2135 | |
| 2136 | name = vim_strnsave(*arg, end - *arg); |
| 2137 | if (name == NULL) |
| 2138 | return FAIL; |
| 2139 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2140 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2141 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2142 | if (!gen_load_outer) |
| 2143 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2144 | } |
| 2145 | else |
| 2146 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2147 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2148 | |
| 2149 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2150 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2151 | type = lvar->lv_type; |
| 2152 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2153 | if (lvar->lv_from_outer) |
| 2154 | gen_load_outer = TRUE; |
| 2155 | else |
| 2156 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2157 | } |
| 2158 | else |
| 2159 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2160 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2161 | // already exists in a Vim9 script or when it's imported. |
| 2162 | if (lookup_script(*arg, len, TRUE) == OK |
| 2163 | || find_imported(name, 0, cctx) != NULL) |
| 2164 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2165 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2166 | // When the name starts with an uppercase letter or "x:" it |
| 2167 | // can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2168 | // TODO: this is just guessing |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2169 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2170 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2171 | } |
| 2172 | } |
| 2173 | if (gen_load) |
| 2174 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2175 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2176 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2177 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2178 | cctx->ctx_outer_used = TRUE; |
| 2179 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | *arg = end; |
| 2183 | |
| 2184 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2185 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2186 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2187 | vim_free(name); |
| 2188 | return res; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Compile the argument expressions. |
| 2193 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2194 | */ |
| 2195 | static int |
| 2196 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2197 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2198 | char_u *p = *arg; |
| 2199 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2200 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2201 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2202 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2203 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2204 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2205 | if (*p == ')') |
| 2206 | { |
| 2207 | *arg = p + 1; |
| 2208 | return OK; |
| 2209 | } |
| 2210 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2211 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2212 | return FAIL; |
| 2213 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2214 | |
| 2215 | if (*p != ',' && *skipwhite(p) == ',') |
| 2216 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2217 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2218 | p = skipwhite(p); |
| 2219 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2220 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2221 | { |
| 2222 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2223 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2224 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2225 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2226 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2227 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2228 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2229 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2230 | emsg(_(e_missing_close)); |
| 2231 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | * Compile a function call: name(arg1, arg2) |
| 2236 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2237 | * "argcount_init" is 1 for "value->method()" |
| 2238 | * Instructions: |
| 2239 | * EVAL arg1 |
| 2240 | * EVAL arg2 |
| 2241 | * BCALL / DCALL / UCALL |
| 2242 | */ |
| 2243 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2244 | compile_call( |
| 2245 | char_u **arg, |
| 2246 | size_t varlen, |
| 2247 | cctx_T *cctx, |
| 2248 | ppconst_T *ppconst, |
| 2249 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2250 | { |
| 2251 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2252 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2253 | int argcount = argcount_init; |
| 2254 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2255 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2256 | char_u *tofree = NULL; |
| 2257 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2258 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2259 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2260 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2261 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2262 | // we can evaluate "has('name')" at compile time |
| 2263 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2264 | { |
| 2265 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2266 | typval_T argvars[2]; |
| 2267 | |
| 2268 | argvars[0].v_type = VAR_UNKNOWN; |
| 2269 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2270 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2271 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2272 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2273 | s = skipwhite(s); |
| 2274 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2275 | { |
| 2276 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2277 | |
| 2278 | *arg = s + 1; |
| 2279 | argvars[1].v_type = VAR_UNKNOWN; |
| 2280 | tv->v_type = VAR_NUMBER; |
| 2281 | tv->vval.v_number = 0; |
| 2282 | f_has(argvars, tv); |
| 2283 | clear_tv(&argvars[0]); |
| 2284 | ++ppconst->pp_used; |
| 2285 | return OK; |
| 2286 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2287 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2291 | return FAIL; |
| 2292 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2293 | if (varlen >= sizeof(namebuf)) |
| 2294 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2295 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2296 | return FAIL; |
| 2297 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2298 | vim_strncpy(namebuf, *arg, varlen); |
| 2299 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2300 | |
| 2301 | *arg = skipwhite(*arg + varlen + 1); |
| 2302 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2303 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2304 | |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2305 | is_autoload = vim_strchr(name, '#') != NULL; |
| 2306 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2307 | { |
| 2308 | int idx; |
| 2309 | |
| 2310 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2311 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2312 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2313 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2314 | else |
| 2315 | semsg(_(e_unknownfunc), namebuf); |
| 2316 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2319 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2320 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2321 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2322 | { |
| 2323 | res = generate_CALL(cctx, ufunc, argcount); |
| 2324 | goto theend; |
| 2325 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2326 | |
| 2327 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2328 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2329 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2330 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2331 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2332 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2333 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2334 | garray_T *stack = &cctx->ctx_type_stack; |
| 2335 | type_T *type; |
| 2336 | |
| 2337 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2338 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2339 | goto theend; |
| 2340 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2341 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2342 | // 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] | 2343 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2344 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2345 | res = generate_UCALL(cctx, name, argcount); |
| 2346 | else |
| 2347 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2348 | |
| 2349 | theend: |
| 2350 | vim_free(tofree); |
| 2351 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2355 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2356 | |
| 2357 | /* |
| 2358 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2359 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2360 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2361 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2362 | * valid name. |
| 2363 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2364 | static char_u * |
| 2365 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2366 | { |
| 2367 | char_u *p; |
| 2368 | |
| 2369 | // Quick check for valid starting character. |
| 2370 | if (!eval_isnamec1(*arg)) |
| 2371 | return arg; |
| 2372 | |
| 2373 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2374 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2375 | // and can be used in slice "[n:]". |
| 2376 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2377 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2378 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2379 | break; |
| 2380 | return p; |
| 2381 | } |
| 2382 | |
| 2383 | /* |
| 2384 | * Like to_name_end() but also skip over a list or dict constant. |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2385 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2386 | */ |
| 2387 | char_u * |
| 2388 | to_name_const_end(char_u *arg) |
| 2389 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2390 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2391 | typval_T rettv; |
| 2392 | |
| 2393 | if (p == arg && *arg == '[') |
| 2394 | { |
| 2395 | |
| 2396 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2397 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2398 | p = arg; |
| 2399 | } |
| 2400 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2401 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2402 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2403 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2404 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2405 | p = arg; |
| 2406 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2407 | |
| 2408 | return p; |
| 2409 | } |
| 2410 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2411 | /* |
| 2412 | * parse a list: [expr, expr] |
| 2413 | * "*arg" points to the '['. |
| 2414 | */ |
| 2415 | static int |
| 2416 | compile_list(char_u **arg, cctx_T *cctx) |
| 2417 | { |
| 2418 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2419 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2420 | int count = 0; |
| 2421 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2422 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2423 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2424 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2425 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2426 | semsg(_(e_list_end), *arg); |
| 2427 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2428 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2429 | if (*p == ',') |
| 2430 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2431 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2432 | return FAIL; |
| 2433 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2434 | if (*p == ']') |
| 2435 | { |
| 2436 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2437 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2438 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2439 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2440 | break; |
| 2441 | ++count; |
| 2442 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2443 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2444 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2445 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2446 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2447 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2448 | return FAIL; |
| 2449 | } |
| 2450 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2451 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2452 | p = skipwhite(p); |
| 2453 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2454 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2455 | |
| 2456 | generate_NEWLIST(cctx, count); |
| 2457 | return OK; |
| 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | * parse a lambda: {arg, arg -> expr} |
| 2462 | * "*arg" points to the '{'. |
| 2463 | */ |
| 2464 | static int |
| 2465 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2466 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2467 | typval_T rettv; |
| 2468 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2469 | evalarg_T evalarg; |
| 2470 | |
| 2471 | CLEAR_FIELD(evalarg); |
| 2472 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2473 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2474 | |
| 2475 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2476 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2477 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2479 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2480 | ++ufunc->uf_refcount; |
| 2481 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2482 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2483 | |
| 2484 | // The function will have one line: "return {expr}". |
| 2485 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2486 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2487 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2488 | clear_evalarg(&evalarg, NULL); |
| 2489 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2490 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2491 | { |
| 2492 | // The return type will now be known. |
| 2493 | set_function_type(ufunc); |
| 2494 | |
| 2495 | return generate_FUNCREF(cctx, ufunc); |
| 2496 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2497 | |
| 2498 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2499 | return FAIL; |
| 2500 | } |
| 2501 | |
| 2502 | /* |
| 2503 | * Compile a lamda call: expr->{lambda}(args) |
| 2504 | * "arg" points to the "{". |
| 2505 | */ |
| 2506 | static int |
| 2507 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2508 | { |
| 2509 | ufunc_T *ufunc; |
| 2510 | typval_T rettv; |
| 2511 | int argcount = 1; |
| 2512 | int ret = FAIL; |
| 2513 | |
| 2514 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2515 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2516 | return FAIL; |
| 2517 | |
| 2518 | if (**arg != '(') |
| 2519 | { |
| 2520 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2521 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2522 | else |
| 2523 | semsg(_(e_missing_paren), "lambda"); |
| 2524 | clear_tv(&rettv); |
| 2525 | return FAIL; |
| 2526 | } |
| 2527 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2528 | ufunc = rettv.vval.v_partial->pt_func; |
| 2529 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2530 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2531 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2532 | |
| 2533 | // The function will have one line: "return {expr}". |
| 2534 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2535 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2536 | |
| 2537 | // compile the arguments |
| 2538 | *arg = skipwhite(*arg + 1); |
| 2539 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2540 | // call the compiled function |
| 2541 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2542 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2543 | if (ret == FAIL) |
| 2544 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2545 | return ret; |
| 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * parse a dict: {'key': val} or #{key: val} |
| 2550 | * "*arg" points to the '{'. |
| 2551 | */ |
| 2552 | static int |
| 2553 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2554 | { |
| 2555 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2556 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2557 | int count = 0; |
| 2558 | dict_T *d = dict_alloc(); |
| 2559 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2560 | char_u *whitep = *arg; |
| 2561 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2562 | |
| 2563 | if (d == NULL) |
| 2564 | return FAIL; |
| 2565 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2566 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2567 | { |
| 2568 | char_u *key = NULL; |
| 2569 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2570 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2571 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2572 | *arg = NULL; |
| 2573 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | if (**arg == '}') |
| 2577 | break; |
| 2578 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2579 | if (literal) |
| 2580 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2581 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2582 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2583 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2584 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2585 | semsg(_(e_invalid_key_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2586 | return FAIL; |
| 2587 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2588 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2589 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2590 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2591 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2592 | } |
| 2593 | else |
| 2594 | { |
| 2595 | isn_T *isn; |
| 2596 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2597 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2598 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2599 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2600 | if (isn->isn_type == ISN_PUSHS) |
| 2601 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2602 | else |
| 2603 | { |
| 2604 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2605 | [stack->ga_len - 1]; |
| 2606 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2607 | return FAIL; |
| 2608 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | // Check for duplicate keys, if using string keys. |
| 2612 | if (key != NULL) |
| 2613 | { |
| 2614 | item = dict_find(d, key, -1); |
| 2615 | if (item != NULL) |
| 2616 | { |
| 2617 | semsg(_(e_duplicate_key), key); |
| 2618 | goto failret; |
| 2619 | } |
| 2620 | item = dictitem_alloc(key); |
| 2621 | if (item != NULL) |
| 2622 | { |
| 2623 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2624 | item->di_tv.v_lock = 0; |
| 2625 | if (dict_add(d, item) == FAIL) |
| 2626 | dictitem_free(item); |
| 2627 | } |
| 2628 | } |
| 2629 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2630 | if (**arg != ':') |
| 2631 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2632 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2633 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2634 | else |
| 2635 | semsg(_(e_missing_dict_colon), *arg); |
| 2636 | return FAIL; |
| 2637 | } |
| 2638 | whitep = *arg + 1; |
| 2639 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 2640 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2641 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2642 | return FAIL; |
| 2643 | } |
| 2644 | |
| 2645 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2646 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2647 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2648 | *arg = NULL; |
| 2649 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2652 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2653 | return FAIL; |
| 2654 | ++count; |
| 2655 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2656 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2657 | *arg = skipwhite(*arg); |
| 2658 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2659 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2660 | *arg = NULL; |
| 2661 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2662 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2663 | if (**arg == '}') |
| 2664 | break; |
| 2665 | if (**arg != ',') |
| 2666 | { |
| 2667 | semsg(_(e_missing_dict_comma), *arg); |
| 2668 | goto failret; |
| 2669 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2670 | if (IS_WHITE_OR_NUL(*whitep)) |
| 2671 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2672 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2673 | return FAIL; |
| 2674 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2675 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2676 | *arg = skipwhite(*arg + 1); |
| 2677 | } |
| 2678 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2679 | *arg = *arg + 1; |
| 2680 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2681 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2682 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2683 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2684 | *arg += STRLEN(*arg); |
| 2685 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2686 | dict_unref(d); |
| 2687 | return generate_NEWDICT(cctx, count); |
| 2688 | |
| 2689 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2690 | if (*arg == NULL) |
| 2691 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2692 | dict_unref(d); |
| 2693 | return FAIL; |
| 2694 | } |
| 2695 | |
| 2696 | /* |
| 2697 | * Compile "&option". |
| 2698 | */ |
| 2699 | static int |
| 2700 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2701 | { |
| 2702 | typval_T rettv; |
| 2703 | char_u *start = *arg; |
| 2704 | int ret; |
| 2705 | |
| 2706 | // parse the option and get the current value to get the type. |
| 2707 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2708 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2709 | if (ret == OK) |
| 2710 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2711 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2712 | char_u *name = vim_strnsave(start, *arg - start); |
| 2713 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2714 | |
| 2715 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2716 | vim_free(name); |
| 2717 | } |
| 2718 | clear_tv(&rettv); |
| 2719 | |
| 2720 | return ret; |
| 2721 | } |
| 2722 | |
| 2723 | /* |
| 2724 | * Compile "$VAR". |
| 2725 | */ |
| 2726 | static int |
| 2727 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2728 | { |
| 2729 | char_u *start = *arg; |
| 2730 | int len; |
| 2731 | int ret; |
| 2732 | char_u *name; |
| 2733 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2734 | ++*arg; |
| 2735 | len = get_env_len(arg); |
| 2736 | if (len == 0) |
| 2737 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2738 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2739 | return FAIL; |
| 2740 | } |
| 2741 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2742 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2743 | name = vim_strnsave(start, len + 1); |
| 2744 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2745 | vim_free(name); |
| 2746 | return ret; |
| 2747 | } |
| 2748 | |
| 2749 | /* |
| 2750 | * Compile "@r". |
| 2751 | */ |
| 2752 | static int |
| 2753 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2754 | { |
| 2755 | int ret; |
| 2756 | |
| 2757 | ++*arg; |
| 2758 | if (**arg == NUL) |
| 2759 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2760 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2761 | return FAIL; |
| 2762 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2763 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2764 | { |
| 2765 | emsg_invreg(**arg); |
| 2766 | return FAIL; |
| 2767 | } |
| 2768 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2769 | ++*arg; |
| 2770 | return ret; |
| 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2775 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2776 | */ |
| 2777 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2778 | apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2779 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2780 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2781 | |
| 2782 | // this works from end to start |
| 2783 | while (p > start) |
| 2784 | { |
| 2785 | --p; |
| 2786 | if (*p == '-' || *p == '+') |
| 2787 | { |
| 2788 | // only '-' has an effect, for '+' we only check the type |
| 2789 | #ifdef FEAT_FLOAT |
| 2790 | if (rettv->v_type == VAR_FLOAT) |
| 2791 | { |
| 2792 | if (*p == '-') |
| 2793 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2794 | } |
| 2795 | else |
| 2796 | #endif |
| 2797 | { |
| 2798 | varnumber_T val; |
| 2799 | int error = FALSE; |
| 2800 | |
| 2801 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2802 | // here |
| 2803 | if (check_not_string(rettv) == FAIL) |
| 2804 | return FAIL; |
| 2805 | val = tv_get_number_chk(rettv, &error); |
| 2806 | clear_tv(rettv); |
| 2807 | if (error) |
| 2808 | return FAIL; |
| 2809 | if (*p == '-') |
| 2810 | val = -val; |
| 2811 | rettv->v_type = VAR_NUMBER; |
| 2812 | rettv->vval.v_number = val; |
| 2813 | } |
| 2814 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2815 | else if (numeric_only) |
| 2816 | { |
| 2817 | ++p; |
| 2818 | break; |
| 2819 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2820 | else |
| 2821 | { |
| 2822 | int v = tv2bool(rettv); |
| 2823 | |
| 2824 | // '!' is permissive in the type. |
| 2825 | clear_tv(rettv); |
| 2826 | rettv->v_type = VAR_BOOL; |
| 2827 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2828 | } |
| 2829 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2830 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2831 | return OK; |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * Recognize v: variables that are constants and set "rettv". |
| 2836 | */ |
| 2837 | static void |
| 2838 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2839 | { |
| 2840 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2841 | { |
| 2842 | rettv->v_type = VAR_BOOL; |
| 2843 | rettv->vval.v_number = VVAL_TRUE; |
| 2844 | *arg += 6; |
| 2845 | } |
| 2846 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2847 | { |
| 2848 | rettv->v_type = VAR_BOOL; |
| 2849 | rettv->vval.v_number = VVAL_FALSE; |
| 2850 | *arg += 7; |
| 2851 | } |
| 2852 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2853 | { |
| 2854 | rettv->v_type = VAR_SPECIAL; |
| 2855 | rettv->vval.v_number = VVAL_NULL; |
| 2856 | *arg += 6; |
| 2857 | } |
| 2858 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2859 | { |
| 2860 | rettv->v_type = VAR_SPECIAL; |
| 2861 | rettv->vval.v_number = VVAL_NONE; |
| 2862 | *arg += 6; |
| 2863 | } |
| 2864 | } |
| 2865 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2866 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2867 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2868 | { |
| 2869 | exptype_T type = EXPR_UNKNOWN; |
| 2870 | int i; |
| 2871 | |
| 2872 | switch (p[0]) |
| 2873 | { |
| 2874 | case '=': if (p[1] == '=') |
| 2875 | type = EXPR_EQUAL; |
| 2876 | else if (p[1] == '~') |
| 2877 | type = EXPR_MATCH; |
| 2878 | break; |
| 2879 | case '!': if (p[1] == '=') |
| 2880 | type = EXPR_NEQUAL; |
| 2881 | else if (p[1] == '~') |
| 2882 | type = EXPR_NOMATCH; |
| 2883 | break; |
| 2884 | case '>': if (p[1] != '=') |
| 2885 | { |
| 2886 | type = EXPR_GREATER; |
| 2887 | *len = 1; |
| 2888 | } |
| 2889 | else |
| 2890 | type = EXPR_GEQUAL; |
| 2891 | break; |
| 2892 | case '<': if (p[1] != '=') |
| 2893 | { |
| 2894 | type = EXPR_SMALLER; |
| 2895 | *len = 1; |
| 2896 | } |
| 2897 | else |
| 2898 | type = EXPR_SEQUAL; |
| 2899 | break; |
| 2900 | case 'i': if (p[1] == 's') |
| 2901 | { |
| 2902 | // "is" and "isnot"; but not a prefix of a name |
| 2903 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2904 | *len = 5; |
| 2905 | i = p[*len]; |
| 2906 | if (!isalnum(i) && i != '_') |
| 2907 | { |
| 2908 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2909 | *type_is = TRUE; |
| 2910 | } |
| 2911 | } |
| 2912 | break; |
| 2913 | } |
| 2914 | return type; |
| 2915 | } |
| 2916 | |
| 2917 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2918 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2919 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2920 | */ |
| 2921 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2922 | compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2923 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2924 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2925 | |
| 2926 | // this works from end to start |
| 2927 | while (p > start) |
| 2928 | { |
| 2929 | --p; |
| 2930 | if (*p == '-' || *p == '+') |
| 2931 | { |
| 2932 | int negate = *p == '-'; |
| 2933 | isn_T *isn; |
| 2934 | |
| 2935 | // TODO: check type |
| 2936 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2937 | { |
| 2938 | --p; |
| 2939 | if (*p == '-') |
| 2940 | negate = !negate; |
| 2941 | } |
| 2942 | // only '-' has an effect, for '+' we only check the type |
| 2943 | if (negate) |
| 2944 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 2945 | else |
| 2946 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 2947 | if (isn == NULL) |
| 2948 | return FAIL; |
| 2949 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2950 | else if (numeric_only) |
| 2951 | { |
| 2952 | ++p; |
| 2953 | break; |
| 2954 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2955 | else |
| 2956 | { |
| 2957 | int invert = TRUE; |
| 2958 | |
| 2959 | while (p > start && p[-1] == '!') |
| 2960 | { |
| 2961 | --p; |
| 2962 | invert = !invert; |
| 2963 | } |
| 2964 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 2965 | return FAIL; |
| 2966 | } |
| 2967 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2968 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2969 | return OK; |
| 2970 | } |
| 2971 | |
| 2972 | /* |
| 2973 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2974 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2975 | */ |
| 2976 | static int |
| 2977 | compile_subscript( |
| 2978 | char_u **arg, |
| 2979 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2980 | char_u *start_leader, |
| 2981 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2982 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2983 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2984 | char_u *name_start = *end_leader; |
| 2985 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2986 | for (;;) |
| 2987 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2988 | char_u *p = skipwhite(*arg); |
| 2989 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2990 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2991 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2992 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2993 | |
| 2994 | // If a following line starts with "->{" or "->X" advance to that |
| 2995 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2996 | // Also if a following line starts with ".x". |
| 2997 | if (next != NULL && |
| 2998 | ((next[0] == '-' && next[1] == '>' |
| 2999 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3000 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3001 | { |
| 3002 | next = next_line_from_context(cctx, TRUE); |
| 3003 | if (next == NULL) |
| 3004 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3005 | *arg = next; |
| 3006 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3010 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3011 | // not a function call. |
| 3012 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3013 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3014 | garray_T *stack = &cctx->ctx_type_stack; |
| 3015 | type_T *type; |
| 3016 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3017 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3018 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3019 | return FAIL; |
| 3020 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3022 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3023 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3024 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3025 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3026 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3027 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3028 | return FAIL; |
| 3029 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3030 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3031 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3032 | char_u *pstart = p; |
| 3033 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3034 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3035 | return FAIL; |
| 3036 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | // something->method() |
| 3038 | // Apply the '!', '-' and '+' first: |
| 3039 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3040 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3041 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3042 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3043 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3044 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3045 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3046 | if (**arg == '{') |
| 3047 | { |
| 3048 | // lambda call: list->{lambda} |
| 3049 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3050 | return FAIL; |
| 3051 | } |
| 3052 | else |
| 3053 | { |
| 3054 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3055 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3056 | if (!eval_isnamec1(*p)) |
| 3057 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3058 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3059 | return FAIL; |
| 3060 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3061 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3062 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3063 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3064 | ; |
| 3065 | if (*p != '(') |
| 3066 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3067 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3068 | return FAIL; |
| 3069 | } |
| 3070 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3071 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3072 | return FAIL; |
| 3073 | } |
| 3074 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3075 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3076 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3077 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3078 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3079 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3080 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3081 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3082 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3083 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3084 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3085 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3086 | // TODO: blob index |
| 3087 | // TODO: more arguments |
| 3088 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3089 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3090 | return FAIL; |
| 3091 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3092 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3093 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3094 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3095 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3096 | if (**arg == ':') |
| 3097 | // missing first index is equal to zero |
| 3098 | generate_PUSHNR(cctx, 0); |
| 3099 | else |
| 3100 | { |
| 3101 | if (compile_expr0(arg, cctx) == FAIL) |
| 3102 | return FAIL; |
| 3103 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3104 | return FAIL; |
| 3105 | *arg = skipwhite(*arg); |
| 3106 | } |
| 3107 | if (**arg == ':') |
| 3108 | { |
| 3109 | *arg = skipwhite(*arg + 1); |
| 3110 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3111 | return FAIL; |
| 3112 | if (**arg == ']') |
| 3113 | // missing second index is equal to end of string |
| 3114 | generate_PUSHNR(cctx, -1); |
| 3115 | else |
| 3116 | { |
| 3117 | if (compile_expr0(arg, cctx) == FAIL) |
| 3118 | return FAIL; |
| 3119 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3120 | return FAIL; |
| 3121 | *arg = skipwhite(*arg); |
| 3122 | } |
| 3123 | is_slice = TRUE; |
| 3124 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3125 | |
| 3126 | if (**arg != ']') |
| 3127 | { |
| 3128 | emsg(_(e_missbrac)); |
| 3129 | return FAIL; |
| 3130 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3131 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3132 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3133 | // We can index a list and a dict. If we don't know the type |
| 3134 | // we can use the index value type. |
| 3135 | // TODO: If we don't know use an instruction to figure it out at |
| 3136 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3137 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3138 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3139 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3140 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3141 | // If the index is a string, the variable must be a Dict. |
| 3142 | if (*typep == &t_any && valtype == &t_string) |
| 3143 | vtype = VAR_DICT; |
| 3144 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3145 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3146 | if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL) |
| 3147 | return FAIL; |
| 3148 | if (is_slice) |
| 3149 | { |
| 3150 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 3151 | if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL) |
| 3152 | return FAIL; |
| 3153 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3154 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3155 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3156 | if (vtype == VAR_DICT) |
| 3157 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3158 | if (is_slice) |
| 3159 | { |
| 3160 | emsg(_(e_cannot_slice_dictionary)); |
| 3161 | return FAIL; |
| 3162 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3163 | if ((*typep)->tt_type == VAR_DICT) |
| 3164 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3165 | else |
| 3166 | { |
| 3167 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3168 | return FAIL; |
| 3169 | *typep = &t_any; |
| 3170 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3171 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3172 | return FAIL; |
| 3173 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3174 | return FAIL; |
| 3175 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3176 | else if (vtype == VAR_STRING) |
| 3177 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3178 | *typep = &t_string; |
| 3179 | if ((is_slice |
| 3180 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3181 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3182 | return FAIL; |
| 3183 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3184 | else if (vtype == VAR_BLOB) |
| 3185 | { |
| 3186 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3187 | return FAIL; |
| 3188 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3189 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3190 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3191 | if (is_slice) |
| 3192 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3193 | if (generate_instr_drop(cctx, |
| 3194 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3195 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3196 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3197 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3198 | else |
| 3199 | { |
| 3200 | if ((*typep)->tt_type == VAR_LIST) |
| 3201 | *typep = (*typep)->tt_member; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3202 | if (generate_instr_drop(cctx, |
| 3203 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3204 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3205 | return FAIL; |
| 3206 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3207 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3208 | else |
| 3209 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3210 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3211 | return FAIL; |
| 3212 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3213 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3214 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3215 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3216 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3217 | return FAIL; |
| 3218 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3219 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3220 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3221 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3222 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3223 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3224 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3225 | while (eval_isnamec(*p)) |
| 3226 | MB_PTR_ADV(p); |
| 3227 | if (p == *arg) |
| 3228 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3229 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3230 | return FAIL; |
| 3231 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3232 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3233 | return FAIL; |
| 3234 | *arg = p; |
| 3235 | } |
| 3236 | else |
| 3237 | break; |
| 3238 | } |
| 3239 | |
| 3240 | // TODO - see handle_subscript(): |
| 3241 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3242 | // Don't do this when "Func" is already a partial that was bound |
| 3243 | // explicitly (pt_auto is FALSE). |
| 3244 | |
| 3245 | return OK; |
| 3246 | } |
| 3247 | |
| 3248 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3249 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3250 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3251 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3252 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3253 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3254 | * |
| 3255 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3256 | */ |
| 3257 | |
| 3258 | /* |
| 3259 | * number number constant |
| 3260 | * 0zFFFFFFFF Blob constant |
| 3261 | * "string" string constant |
| 3262 | * 'string' literal string constant |
| 3263 | * &option-name option value |
| 3264 | * @r register contents |
| 3265 | * identifier variable value |
| 3266 | * function() function call |
| 3267 | * $VAR environment variable |
| 3268 | * (expression) nested expression |
| 3269 | * [expr, expr] List |
| 3270 | * {key: val, key: val} Dictionary |
| 3271 | * #{key: val, key: val} Dictionary with literal keys |
| 3272 | * |
| 3273 | * Also handle: |
| 3274 | * ! in front logical NOT |
| 3275 | * - in front unary minus |
| 3276 | * + in front unary plus (ignored) |
| 3277 | * trailing (arg) funcref/partial call |
| 3278 | * trailing [] subscript in String or List |
| 3279 | * trailing .name entry in Dictionary |
| 3280 | * trailing ->name() method call |
| 3281 | */ |
| 3282 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3283 | compile_expr7( |
| 3284 | char_u **arg, |
| 3285 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3286 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3287 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3288 | char_u *start_leader, *end_leader; |
| 3289 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3290 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3291 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3292 | |
| 3293 | /* |
| 3294 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3295 | */ |
| 3296 | start_leader = *arg; |
| 3297 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3298 | *arg = skipwhite(*arg + 1); |
| 3299 | end_leader = *arg; |
| 3300 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3301 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3302 | switch (**arg) |
| 3303 | { |
| 3304 | /* |
| 3305 | * Number constant. |
| 3306 | */ |
| 3307 | case '0': // also for blob starting with 0z |
| 3308 | case '1': |
| 3309 | case '2': |
| 3310 | case '3': |
| 3311 | case '4': |
| 3312 | case '5': |
| 3313 | case '6': |
| 3314 | case '7': |
| 3315 | case '8': |
| 3316 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3317 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3318 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3319 | // Apply "-" and "+" just before the number now, right to |
| 3320 | // left. Matters especially when "->" follows. Stops at |
| 3321 | // '!'. |
| 3322 | if (apply_leader(rettv, TRUE, |
| 3323 | start_leader, &end_leader) == FAIL) |
| 3324 | { |
| 3325 | clear_tv(rettv); |
| 3326 | return FAIL; |
| 3327 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3328 | break; |
| 3329 | |
| 3330 | /* |
| 3331 | * String constant: "string". |
| 3332 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3333 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3334 | return FAIL; |
| 3335 | break; |
| 3336 | |
| 3337 | /* |
| 3338 | * Literal string constant: 'str''ing'. |
| 3339 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3340 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3341 | return FAIL; |
| 3342 | break; |
| 3343 | |
| 3344 | /* |
| 3345 | * Constant Vim variable. |
| 3346 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3347 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3348 | ret = NOTDONE; |
| 3349 | break; |
| 3350 | |
| 3351 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3352 | * "true" constant |
| 3353 | */ |
| 3354 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3355 | && !eval_isnamec((*arg)[4])) |
| 3356 | { |
| 3357 | *arg += 4; |
| 3358 | rettv->v_type = VAR_BOOL; |
| 3359 | rettv->vval.v_number = VVAL_TRUE; |
| 3360 | } |
| 3361 | else |
| 3362 | ret = NOTDONE; |
| 3363 | break; |
| 3364 | |
| 3365 | /* |
| 3366 | * "false" constant |
| 3367 | */ |
| 3368 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3369 | && !eval_isnamec((*arg)[5])) |
| 3370 | { |
| 3371 | *arg += 5; |
| 3372 | rettv->v_type = VAR_BOOL; |
| 3373 | rettv->vval.v_number = VVAL_FALSE; |
| 3374 | } |
| 3375 | else |
| 3376 | ret = NOTDONE; |
| 3377 | break; |
| 3378 | |
| 3379 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3380 | * List: [expr, expr] |
| 3381 | */ |
| 3382 | case '[': ret = compile_list(arg, cctx); |
| 3383 | break; |
| 3384 | |
| 3385 | /* |
| 3386 | * Dictionary: #{key: val, key: val} |
| 3387 | */ |
| 3388 | case '#': if ((*arg)[1] == '{') |
| 3389 | { |
| 3390 | ++*arg; |
| 3391 | ret = compile_dict(arg, cctx, TRUE); |
| 3392 | } |
| 3393 | else |
| 3394 | ret = NOTDONE; |
| 3395 | break; |
| 3396 | |
| 3397 | /* |
| 3398 | * Lambda: {arg, arg -> expr} |
| 3399 | * Dictionary: {'key': val, 'key': val} |
| 3400 | */ |
| 3401 | case '{': { |
| 3402 | char_u *start = skipwhite(*arg + 1); |
| 3403 | |
| 3404 | // Find out what comes after the arguments. |
| 3405 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3406 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3407 | if (ret != FAIL && *start == '>') |
| 3408 | ret = compile_lambda(arg, cctx); |
| 3409 | else |
| 3410 | ret = compile_dict(arg, cctx, FALSE); |
| 3411 | } |
| 3412 | break; |
| 3413 | |
| 3414 | /* |
| 3415 | * Option value: &name |
| 3416 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3417 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3418 | return FAIL; |
| 3419 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3420 | break; |
| 3421 | |
| 3422 | /* |
| 3423 | * Environment variable: $VAR. |
| 3424 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3425 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3426 | return FAIL; |
| 3427 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3428 | break; |
| 3429 | |
| 3430 | /* |
| 3431 | * Register contents: @r. |
| 3432 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3433 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3434 | return FAIL; |
| 3435 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3436 | break; |
| 3437 | /* |
| 3438 | * nested expression: (expression). |
| 3439 | */ |
| 3440 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3441 | |
| 3442 | // recursive! |
| 3443 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3444 | { |
| 3445 | ret = compile_expr1(arg, cctx, ppconst); |
| 3446 | } |
| 3447 | else |
| 3448 | { |
| 3449 | // Not enough space in ppconst, flush constants. |
| 3450 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3451 | return FAIL; |
| 3452 | ret = compile_expr0(arg, cctx); |
| 3453 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3454 | *arg = skipwhite(*arg); |
| 3455 | if (**arg == ')') |
| 3456 | ++*arg; |
| 3457 | else if (ret == OK) |
| 3458 | { |
| 3459 | emsg(_(e_missing_close)); |
| 3460 | ret = FAIL; |
| 3461 | } |
| 3462 | break; |
| 3463 | |
| 3464 | default: ret = NOTDONE; |
| 3465 | break; |
| 3466 | } |
| 3467 | if (ret == FAIL) |
| 3468 | return FAIL; |
| 3469 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3470 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3471 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3472 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3473 | clear_tv(rettv); |
| 3474 | else |
| 3475 | // A constant expression can possibly be handled compile time, |
| 3476 | // return the value instead of generating code. |
| 3477 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3478 | } |
| 3479 | else if (ret == NOTDONE) |
| 3480 | { |
| 3481 | char_u *p; |
| 3482 | int r; |
| 3483 | |
| 3484 | if (!eval_isnamec1(**arg)) |
| 3485 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3486 | semsg(_(e_name_expected), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3487 | return FAIL; |
| 3488 | } |
| 3489 | |
| 3490 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3491 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3492 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3493 | { |
| 3494 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3495 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3496 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3497 | { |
| 3498 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3499 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3500 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3501 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3502 | if (r == FAIL) |
| 3503 | return FAIL; |
| 3504 | } |
| 3505 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3506 | // Handle following "[]", ".member", etc. |
| 3507 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3508 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3509 | ppconst) == FAIL) |
| 3510 | return FAIL; |
| 3511 | if (ppconst->pp_used > 0) |
| 3512 | { |
| 3513 | // apply the '!', '-' and '+' before the constant |
| 3514 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3515 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3516 | return FAIL; |
| 3517 | return OK; |
| 3518 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3519 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3520 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3521 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3525 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3526 | */ |
| 3527 | void |
| 3528 | error_white_both(char_u *op, int len) |
| 3529 | { |
| 3530 | char_u buf[10]; |
| 3531 | |
| 3532 | vim_strncpy(buf, op, len); |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3533 | semsg(_(e_white_space_required_before_and_after_str), buf); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3537 | * <type>expr7: runtime type check / conversion |
| 3538 | */ |
| 3539 | static int |
| 3540 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3541 | { |
| 3542 | type_T *want_type = NULL; |
| 3543 | |
| 3544 | // Recognize <type> |
| 3545 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3546 | { |
| 3547 | int called_emsg_before = called_emsg; |
| 3548 | |
| 3549 | ++*arg; |
| 3550 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3551 | if (called_emsg != called_emsg_before) |
| 3552 | return FAIL; |
| 3553 | |
| 3554 | if (**arg != '>') |
| 3555 | { |
| 3556 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3557 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3558 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3559 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3560 | return FAIL; |
| 3561 | } |
| 3562 | ++*arg; |
| 3563 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3564 | return FAIL; |
| 3565 | } |
| 3566 | |
| 3567 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3568 | return FAIL; |
| 3569 | |
| 3570 | if (want_type != NULL) |
| 3571 | { |
| 3572 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3573 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3574 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3575 | generate_ppconst(cctx, ppconst); |
| 3576 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3577 | if (check_type(want_type, actual, FALSE) == FAIL) |
| 3578 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3579 | if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL) |
| 3580 | return FAIL; |
| 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | return OK; |
| 3585 | } |
| 3586 | |
| 3587 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3588 | * * number multiplication |
| 3589 | * / number division |
| 3590 | * % number modulo |
| 3591 | */ |
| 3592 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3593 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3594 | { |
| 3595 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3596 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3597 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3598 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3599 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3600 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3601 | return FAIL; |
| 3602 | |
| 3603 | /* |
| 3604 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3605 | */ |
| 3606 | for (;;) |
| 3607 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3608 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3609 | if (*op != '*' && *op != '/' && *op != '%') |
| 3610 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3611 | if (next != NULL) |
| 3612 | { |
| 3613 | *arg = next_line_from_context(cctx, TRUE); |
| 3614 | op = skipwhite(*arg); |
| 3615 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3616 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3617 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3618 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3619 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3620 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3621 | } |
| 3622 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3623 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3624 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3625 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3626 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3627 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3628 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3629 | |
| 3630 | if (ppconst->pp_used == ppconst_used + 2 |
| 3631 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3632 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3633 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3634 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3635 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3636 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3637 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3638 | // both are numbers: compute the result |
| 3639 | switch (*op) |
| 3640 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3641 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3642 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3643 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3644 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3645 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3646 | break; |
| 3647 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3648 | tv1->vval.v_number = res; |
| 3649 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3650 | } |
| 3651 | else |
| 3652 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3653 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3654 | generate_two_op(cctx, op); |
| 3655 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | return OK; |
| 3659 | } |
| 3660 | |
| 3661 | /* |
| 3662 | * + number addition |
| 3663 | * - number subtraction |
| 3664 | * .. string concatenation |
| 3665 | */ |
| 3666 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3667 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3668 | { |
| 3669 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3670 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3671 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3672 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3673 | |
| 3674 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3675 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3676 | return FAIL; |
| 3677 | |
| 3678 | /* |
| 3679 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3680 | */ |
| 3681 | for (;;) |
| 3682 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3683 | op = may_peek_next_line(cctx, *arg, &next); |
| 3684 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3685 | break; |
| 3686 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3687 | if (next != NULL) |
| 3688 | { |
| 3689 | *arg = next_line_from_context(cctx, TRUE); |
| 3690 | op = skipwhite(*arg); |
| 3691 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3692 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3693 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3694 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3695 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3696 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3700 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3701 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3702 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3703 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3704 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3705 | return FAIL; |
| 3706 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3707 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3708 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3709 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3710 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3711 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3712 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3713 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3714 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3715 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3716 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3717 | // concat/subtract/add constant numbers |
| 3718 | if (*op == '+') |
| 3719 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3720 | else if (*op == '-') |
| 3721 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3722 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3723 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3724 | // concatenate constant strings |
| 3725 | char_u *s1 = tv1->vval.v_string; |
| 3726 | char_u *s2 = tv2->vval.v_string; |
| 3727 | size_t len1 = STRLEN(s1); |
| 3728 | |
| 3729 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3730 | if (tv1->vval.v_string == NULL) |
| 3731 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3732 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3733 | return FAIL; |
| 3734 | } |
| 3735 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3736 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3737 | vim_free(s1); |
| 3738 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3739 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3740 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3741 | } |
| 3742 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3743 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3744 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3745 | if (*op == '.') |
| 3746 | { |
| 3747 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3748 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3749 | return FAIL; |
| 3750 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3751 | } |
| 3752 | else |
| 3753 | generate_two_op(cctx, op); |
| 3754 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | return OK; |
| 3758 | } |
| 3759 | |
| 3760 | /* |
| 3761 | * expr5a == expr5b |
| 3762 | * expr5a =~ expr5b |
| 3763 | * expr5a != expr5b |
| 3764 | * expr5a !~ expr5b |
| 3765 | * expr5a > expr5b |
| 3766 | * expr5a >= expr5b |
| 3767 | * expr5a < expr5b |
| 3768 | * expr5a <= expr5b |
| 3769 | * expr5a is expr5b |
| 3770 | * expr5a isnot expr5b |
| 3771 | * |
| 3772 | * Produces instructions: |
| 3773 | * EVAL expr5a Push result of "expr5a" |
| 3774 | * EVAL expr5b Push result of "expr5b" |
| 3775 | * COMPARE one of the compare instructions |
| 3776 | */ |
| 3777 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3778 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3779 | { |
| 3780 | exptype_T type = EXPR_UNKNOWN; |
| 3781 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3782 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3783 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3784 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3785 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3786 | |
| 3787 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3788 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3789 | return FAIL; |
| 3790 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3791 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3792 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3793 | |
| 3794 | /* |
| 3795 | * If there is a comparative operator, use it. |
| 3796 | */ |
| 3797 | if (type != EXPR_UNKNOWN) |
| 3798 | { |
| 3799 | int ic = FALSE; // Default: do not ignore case |
| 3800 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3801 | if (next != NULL) |
| 3802 | { |
| 3803 | *arg = next_line_from_context(cctx, TRUE); |
| 3804 | p = skipwhite(*arg); |
| 3805 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3806 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3807 | { |
| 3808 | semsg(_(e_invexpr2), *arg); |
| 3809 | return FAIL; |
| 3810 | } |
| 3811 | // extra question mark appended: ignore case |
| 3812 | if (p[len] == '?') |
| 3813 | { |
| 3814 | ic = TRUE; |
| 3815 | ++len; |
| 3816 | } |
| 3817 | // extra '#' appended: match case (ignored) |
| 3818 | else if (p[len] == '#') |
| 3819 | ++len; |
| 3820 | // nothing appended: match case |
| 3821 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3822 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3823 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3824 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3825 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | // get the second variable |
| 3829 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3830 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3831 | return FAIL; |
| 3832 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3833 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3834 | return FAIL; |
| 3835 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3836 | if (ppconst->pp_used == ppconst_used + 2) |
| 3837 | { |
| 3838 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3839 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3840 | int ret; |
| 3841 | |
| 3842 | // Both sides are a constant, compute the result now. |
| 3843 | // First check for a valid combination of types, this is more |
| 3844 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3845 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3846 | ret = FAIL; |
| 3847 | else |
| 3848 | { |
| 3849 | ret = typval_compare(tv1, tv2, type, ic); |
| 3850 | tv1->v_type = VAR_BOOL; |
| 3851 | tv1->vval.v_number = tv1->vval.v_number |
| 3852 | ? VVAL_TRUE : VVAL_FALSE; |
| 3853 | clear_tv(tv2); |
| 3854 | --ppconst->pp_used; |
| 3855 | } |
| 3856 | return ret; |
| 3857 | } |
| 3858 | |
| 3859 | generate_ppconst(cctx, ppconst); |
| 3860 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | return OK; |
| 3864 | } |
| 3865 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3866 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3867 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3868 | /* |
| 3869 | * Compile || or &&. |
| 3870 | */ |
| 3871 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3872 | compile_and_or( |
| 3873 | char_u **arg, |
| 3874 | cctx_T *cctx, |
| 3875 | char *op, |
| 3876 | ppconst_T *ppconst, |
| 3877 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3878 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3879 | char_u *next; |
| 3880 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | int opchar = *op; |
| 3882 | |
| 3883 | if (p[0] == opchar && p[1] == opchar) |
| 3884 | { |
| 3885 | garray_T *instr = &cctx->ctx_instr; |
| 3886 | garray_T end_ga; |
| 3887 | |
| 3888 | /* |
| 3889 | * Repeat until there is no following "||" or "&&" |
| 3890 | */ |
| 3891 | ga_init2(&end_ga, sizeof(int), 10); |
| 3892 | while (p[0] == opchar && p[1] == opchar) |
| 3893 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3894 | if (next != NULL) |
| 3895 | { |
| 3896 | *arg = next_line_from_context(cctx, TRUE); |
| 3897 | p = skipwhite(*arg); |
| 3898 | } |
| 3899 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3900 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3901 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3902 | semsg(_(e_white_space_required_before_and_after_str), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3903 | return FAIL; |
| 3904 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3905 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3906 | // TODO: use ppconst if the value is a constant |
| 3907 | generate_ppconst(cctx, ppconst); |
| 3908 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3909 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3910 | { |
| 3911 | ga_clear(&end_ga); |
| 3912 | return FAIL; |
| 3913 | } |
| 3914 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3915 | ++end_ga.ga_len; |
| 3916 | generate_JUMP(cctx, opchar == '|' |
| 3917 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3918 | |
| 3919 | // eval the next expression |
| 3920 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3921 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3922 | return FAIL; |
| 3923 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3924 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3925 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3926 | { |
| 3927 | ga_clear(&end_ga); |
| 3928 | return FAIL; |
| 3929 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3930 | |
| 3931 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3932 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3933 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3934 | |
| 3935 | // Fill in the end label in all jumps. |
| 3936 | while (end_ga.ga_len > 0) |
| 3937 | { |
| 3938 | isn_T *isn; |
| 3939 | |
| 3940 | --end_ga.ga_len; |
| 3941 | isn = ((isn_T *)instr->ga_data) |
| 3942 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3943 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3944 | } |
| 3945 | ga_clear(&end_ga); |
| 3946 | } |
| 3947 | |
| 3948 | return OK; |
| 3949 | } |
| 3950 | |
| 3951 | /* |
| 3952 | * expr4a && expr4a && expr4a logical AND |
| 3953 | * |
| 3954 | * Produces instructions: |
| 3955 | * EVAL expr4a Push result of "expr4a" |
| 3956 | * JUMP_AND_KEEP_IF_FALSE end |
| 3957 | * EVAL expr4b Push result of "expr4b" |
| 3958 | * JUMP_AND_KEEP_IF_FALSE end |
| 3959 | * EVAL expr4c Push result of "expr4c" |
| 3960 | * end: |
| 3961 | */ |
| 3962 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3963 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3964 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3965 | int ppconst_used = ppconst->pp_used; |
| 3966 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3967 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3968 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3969 | return FAIL; |
| 3970 | |
| 3971 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3972 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | /* |
| 3976 | * expr3a || expr3b || expr3c logical OR |
| 3977 | * |
| 3978 | * Produces instructions: |
| 3979 | * EVAL expr3a Push result of "expr3a" |
| 3980 | * JUMP_AND_KEEP_IF_TRUE end |
| 3981 | * EVAL expr3b Push result of "expr3b" |
| 3982 | * JUMP_AND_KEEP_IF_TRUE end |
| 3983 | * EVAL expr3c Push result of "expr3c" |
| 3984 | * end: |
| 3985 | */ |
| 3986 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3987 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3988 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3989 | int ppconst_used = ppconst->pp_used; |
| 3990 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3991 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3992 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3993 | return FAIL; |
| 3994 | |
| 3995 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3996 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4001 | * |
| 4002 | * Produces instructions: |
| 4003 | * EVAL expr2 Push result of "expr" |
| 4004 | * JUMP_IF_FALSE alt jump if false |
| 4005 | * EVAL expr1a |
| 4006 | * JUMP_ALWAYS end |
| 4007 | * alt: EVAL expr1b |
| 4008 | * end: |
| 4009 | */ |
| 4010 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4011 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4012 | { |
| 4013 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4014 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4015 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4016 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4017 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4018 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4019 | return FAIL; |
| 4020 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4021 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4022 | if (*p == '?') |
| 4023 | { |
| 4024 | garray_T *instr = &cctx->ctx_instr; |
| 4025 | garray_T *stack = &cctx->ctx_type_stack; |
| 4026 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4027 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4028 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4029 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4030 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4031 | int has_const_expr = FALSE; |
| 4032 | int const_value = FALSE; |
| 4033 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4034 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4035 | if (next != NULL) |
| 4036 | { |
| 4037 | *arg = next_line_from_context(cctx, TRUE); |
| 4038 | p = skipwhite(*arg); |
| 4039 | } |
| 4040 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4041 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4042 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4043 | semsg(_(e_white_space_required_before_and_after_str), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4044 | return FAIL; |
| 4045 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4046 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4047 | if (ppconst->pp_used == ppconst_used + 1) |
| 4048 | { |
| 4049 | // the condition is a constant, we know whether the ? or the : |
| 4050 | // expression is to be evaluated. |
| 4051 | has_const_expr = TRUE; |
| 4052 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4053 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4054 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4055 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4056 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4057 | } |
| 4058 | else |
| 4059 | { |
| 4060 | generate_ppconst(cctx, ppconst); |
| 4061 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4062 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4063 | |
| 4064 | // evaluate the second expression; any type is accepted |
| 4065 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4066 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4067 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4068 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4069 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4070 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4071 | if (!has_const_expr) |
| 4072 | { |
| 4073 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4074 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4075 | // remember the type and drop it |
| 4076 | --stack->ga_len; |
| 4077 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4078 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4079 | end_idx = instr->ga_len; |
| 4080 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4081 | |
| 4082 | // jump here from JUMP_IF_FALSE |
| 4083 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4084 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4085 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | |
| 4087 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4088 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4089 | if (*p != ':') |
| 4090 | { |
| 4091 | emsg(_(e_missing_colon)); |
| 4092 | return FAIL; |
| 4093 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4094 | if (next != NULL) |
| 4095 | { |
| 4096 | *arg = next_line_from_context(cctx, TRUE); |
| 4097 | p = skipwhite(*arg); |
| 4098 | } |
| 4099 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4100 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4101 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4102 | semsg(_(e_white_space_required_before_and_after_str), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4103 | return FAIL; |
| 4104 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4105 | |
| 4106 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4107 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4108 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4109 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4110 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4111 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4112 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4113 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4114 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4115 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4116 | if (!has_const_expr) |
| 4117 | { |
| 4118 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4119 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4120 | // If the types differ, the result has a more generic type. |
| 4121 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4122 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4123 | |
| 4124 | // jump here from JUMP_ALWAYS |
| 4125 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4126 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4127 | } |
| 4128 | |
| 4129 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4130 | } |
| 4131 | return OK; |
| 4132 | } |
| 4133 | |
| 4134 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4135 | * Toplevel expression. |
| 4136 | */ |
| 4137 | static int |
| 4138 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4139 | { |
| 4140 | ppconst_T ppconst; |
| 4141 | |
| 4142 | CLEAR_FIELD(ppconst); |
| 4143 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4144 | { |
| 4145 | clear_ppconst(&ppconst); |
| 4146 | return FAIL; |
| 4147 | } |
| 4148 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4149 | return FAIL; |
| 4150 | return OK; |
| 4151 | } |
| 4152 | |
| 4153 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4154 | * compile "return [expr]" |
| 4155 | */ |
| 4156 | static char_u * |
| 4157 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4158 | { |
| 4159 | char_u *p = arg; |
| 4160 | garray_T *stack = &cctx->ctx_type_stack; |
| 4161 | type_T *stack_type; |
| 4162 | |
| 4163 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4164 | { |
| 4165 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4166 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4167 | return NULL; |
| 4168 | |
| 4169 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4170 | if (set_return_type) |
| 4171 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4172 | else |
| 4173 | { |
| 4174 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4175 | && stack_type->tt_type != VAR_VOID |
| 4176 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4177 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4178 | emsg(_(e_returning_value_in_function_without_return_type)); |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4179 | return NULL; |
| 4180 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4181 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4182 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4183 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4184 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4185 | } |
| 4186 | else |
| 4187 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4188 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4189 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4190 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4191 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4192 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4193 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4194 | return NULL; |
| 4195 | } |
| 4196 | |
| 4197 | // No argument, return zero. |
| 4198 | generate_PUSHNR(cctx, 0); |
| 4199 | } |
| 4200 | |
| 4201 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4202 | return NULL; |
| 4203 | |
| 4204 | // "return val | endif" is possible |
| 4205 | return skipwhite(p); |
| 4206 | } |
| 4207 | |
| 4208 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4209 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4210 | * Return a pointer to the line in allocated memory. |
| 4211 | * Return NULL for end-of-file or some error. |
| 4212 | */ |
| 4213 | static char_u * |
| 4214 | exarg_getline( |
| 4215 | int c UNUSED, |
| 4216 | void *cookie, |
| 4217 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4218 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4219 | { |
| 4220 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4221 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4222 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4223 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4224 | { |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4225 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4226 | return NULL; |
| 4227 | ++cctx->ctx_lnum; |
| 4228 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4229 | // Comment lines result in NULL pointers, skip them. |
| 4230 | if (p != NULL) |
| 4231 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4232 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | /* |
| 4236 | * Compile a nested :def command. |
| 4237 | */ |
| 4238 | static char_u * |
| 4239 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4240 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4241 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4242 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4243 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4244 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4245 | lvar_T *lvar; |
| 4246 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4247 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4248 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4249 | // Only g:Func() can use a namespace. |
| 4250 | if (name_start[1] == ':' && !is_global) |
| 4251 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4252 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4253 | return NULL; |
| 4254 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4255 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4256 | return NULL; |
| 4257 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4258 | eap->arg = name_end; |
| 4259 | eap->getline = exarg_getline; |
| 4260 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4261 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4262 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4263 | lambda_name = get_lambda_name(); |
| 4264 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4265 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4266 | if (ufunc == NULL) |
| 4267 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4268 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4269 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4270 | return NULL; |
| 4271 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4272 | if (is_global) |
| 4273 | { |
| 4274 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4275 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4276 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4277 | if (func_name == NULL) |
| 4278 | r = FAIL; |
| 4279 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4280 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4281 | } |
| 4282 | else |
| 4283 | { |
| 4284 | // Define a local variable for the function reference. |
| 4285 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4286 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4287 | if (lvar == NULL) |
| 4288 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4289 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4290 | return NULL; |
| 4291 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4292 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4293 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4294 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4295 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4299 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4300 | */ |
| 4301 | int |
| 4302 | assignment_len(char_u *p, int *heredoc) |
| 4303 | { |
| 4304 | if (*p == '=') |
| 4305 | { |
| 4306 | if (p[1] == '<' && p[2] == '<') |
| 4307 | { |
| 4308 | *heredoc = TRUE; |
| 4309 | return 3; |
| 4310 | } |
| 4311 | return 1; |
| 4312 | } |
| 4313 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4314 | return 2; |
| 4315 | if (STRNCMP(p, "..=", 3) == 0) |
| 4316 | return 3; |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
| 4320 | // words that cannot be used as a variable |
| 4321 | static char *reserved[] = { |
| 4322 | "true", |
| 4323 | "false", |
| 4324 | NULL |
| 4325 | }; |
| 4326 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4327 | typedef enum { |
| 4328 | dest_local, |
| 4329 | dest_option, |
| 4330 | dest_env, |
| 4331 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4332 | dest_buffer, |
| 4333 | dest_window, |
| 4334 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4335 | dest_vimvar, |
| 4336 | dest_script, |
| 4337 | dest_reg, |
| 4338 | } assign_dest_T; |
| 4339 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4340 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4341 | * Generate the load instruction for "name". |
| 4342 | */ |
| 4343 | static void |
| 4344 | generate_loadvar( |
| 4345 | cctx_T *cctx, |
| 4346 | assign_dest_T dest, |
| 4347 | char_u *name, |
| 4348 | lvar_T *lvar, |
| 4349 | type_T *type) |
| 4350 | { |
| 4351 | switch (dest) |
| 4352 | { |
| 4353 | case dest_option: |
| 4354 | // TODO: check the option exists |
| 4355 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4356 | break; |
| 4357 | case dest_global: |
| 4358 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4359 | break; |
| 4360 | case dest_buffer: |
| 4361 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4362 | break; |
| 4363 | case dest_window: |
| 4364 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4365 | break; |
| 4366 | case dest_tab: |
| 4367 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4368 | break; |
| 4369 | case dest_script: |
| 4370 | compile_load_scriptvar(cctx, |
| 4371 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4372 | break; |
| 4373 | case dest_env: |
| 4374 | // Include $ in the name here |
| 4375 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4376 | break; |
| 4377 | case dest_reg: |
| 4378 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4379 | break; |
| 4380 | case dest_vimvar: |
| 4381 | generate_LOADV(cctx, name + 2, TRUE); |
| 4382 | break; |
| 4383 | case dest_local: |
| 4384 | if (lvar->lv_from_outer) |
| 4385 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4386 | NULL, type); |
| 4387 | else |
| 4388 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4389 | break; |
| 4390 | } |
| 4391 | } |
| 4392 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4393 | void |
| 4394 | vim9_declare_error(char_u *name) |
| 4395 | { |
| 4396 | char *scope = ""; |
| 4397 | |
| 4398 | switch (*name) |
| 4399 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4400 | case 'g': scope = _("global"); break; |
| 4401 | case 'b': scope = _("buffer"); break; |
| 4402 | case 'w': scope = _("window"); break; |
| 4403 | case 't': scope = _("tab"); break; |
| 4404 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4405 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4406 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4407 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4408 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4409 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4410 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4411 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4412 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4413 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4414 | } |
| 4415 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4416 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4417 | * Compile declaration and assignment: |
| 4418 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4419 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4420 | * Return NULL for an error. |
| 4421 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4422 | */ |
| 4423 | static char_u * |
| 4424 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4425 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4426 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4427 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4428 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4429 | char_u *ret = NULL; |
| 4430 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4431 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4432 | int scriptvar_sid = 0; |
| 4433 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4434 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4435 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4436 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4437 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4438 | int oplen = 0; |
| 4439 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4440 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4441 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4442 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4443 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4444 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4445 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4446 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4447 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4448 | if (p == NULL) |
| 4449 | return *arg == '[' ? arg : NULL; |
| 4450 | |
| 4451 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4452 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4453 | // TODO: should we allow this, and figure out type inference from list |
| 4454 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4455 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4456 | return NULL; |
| 4457 | } |
| 4458 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4459 | sp = p; |
| 4460 | p = skipwhite(p); |
| 4461 | op = p; |
| 4462 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4463 | |
| 4464 | if (var_count > 0 && oplen == 0) |
| 4465 | // can be something like "[1, 2]->func()" |
| 4466 | return arg; |
| 4467 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4468 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4469 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4470 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4471 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4472 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4473 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4474 | if (heredoc) |
| 4475 | { |
| 4476 | list_T *l; |
| 4477 | listitem_T *li; |
| 4478 | |
| 4479 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4480 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4481 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4482 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4483 | |
| 4484 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4485 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4486 | { |
| 4487 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4488 | li->li_tv.vval.v_string = NULL; |
| 4489 | } |
| 4490 | generate_NEWLIST(cctx, l->lv_len); |
| 4491 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4492 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4493 | list_free(l); |
| 4494 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4495 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4496 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4497 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4498 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4499 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4500 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4501 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4502 | p = skipwhite(op + oplen); |
| 4503 | if (compile_expr0(&p, cctx) == FAIL) |
| 4504 | return NULL; |
| 4505 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4506 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4507 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4508 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4509 | type_T *stacktype; |
| 4510 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4511 | stacktype = stack->ga_len == 0 ? &t_void |
| 4512 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4513 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4514 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4515 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4516 | goto theend; |
| 4517 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4518 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4519 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4520 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4521 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4522 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4523 | } |
| 4524 | } |
| 4525 | |
| 4526 | /* |
| 4527 | * Loop over variables in "[var, var] = expr". |
| 4528 | * For "var = expr" and "let var: type" this is done only once. |
| 4529 | */ |
| 4530 | if (var_count > 0) |
| 4531 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4532 | else |
| 4533 | var_start = arg; |
| 4534 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4535 | { |
| 4536 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4537 | size_t varlen; |
| 4538 | int new_local = FALSE; |
| 4539 | int opt_type; |
| 4540 | int opt_flags = 0; |
| 4541 | assign_dest_T dest = dest_local; |
| 4542 | int vimvaridx = -1; |
| 4543 | lvar_T *lvar = NULL; |
| 4544 | lvar_T arg_lvar; |
| 4545 | int has_type = FALSE; |
| 4546 | int has_index = FALSE; |
| 4547 | int instr_count = -1; |
| 4548 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4549 | if (*var_start == '@') |
| 4550 | p = var_start + 2; |
| 4551 | else |
| 4552 | { |
| 4553 | p = (*var_start == '&' || *var_start == '$') |
| 4554 | ? var_start + 1 : var_start; |
| 4555 | p = to_name_end(p, TRUE); |
| 4556 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4557 | |
| 4558 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4559 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4560 | --var_end; |
| 4561 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4562 | --p; |
| 4563 | |
| 4564 | varlen = p - var_start; |
| 4565 | vim_free(name); |
| 4566 | name = vim_strnsave(var_start, varlen); |
| 4567 | if (name == NULL) |
| 4568 | return NULL; |
| 4569 | if (!heredoc) |
| 4570 | type = &t_any; |
| 4571 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4572 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4573 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4574 | int declare_error = FALSE; |
| 4575 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4576 | if (*var_start == '&') |
| 4577 | { |
| 4578 | int cc; |
| 4579 | long numval; |
| 4580 | |
| 4581 | dest = dest_option; |
| 4582 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4583 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4584 | emsg(_(e_const_option)); |
| 4585 | goto theend; |
| 4586 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4587 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4588 | p = var_start; |
| 4589 | p = find_option_end(&p, &opt_flags); |
| 4590 | if (p == NULL) |
| 4591 | { |
| 4592 | // cannot happen? |
| 4593 | emsg(_(e_letunexp)); |
| 4594 | goto theend; |
| 4595 | } |
| 4596 | cc = *p; |
| 4597 | *p = NUL; |
| 4598 | opt_type = get_option_value(var_start + 1, &numval, |
| 4599 | NULL, opt_flags); |
| 4600 | *p = cc; |
| 4601 | if (opt_type == -3) |
| 4602 | { |
| 4603 | semsg(_(e_unknown_option), var_start); |
| 4604 | goto theend; |
| 4605 | } |
| 4606 | if (opt_type == -2 || opt_type == 0) |
| 4607 | type = &t_string; |
| 4608 | else |
| 4609 | type = &t_number; // both number and boolean option |
| 4610 | } |
| 4611 | else if (*var_start == '$') |
| 4612 | { |
| 4613 | dest = dest_env; |
| 4614 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4615 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4616 | } |
| 4617 | else if (*var_start == '@') |
| 4618 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4619 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4620 | { |
| 4621 | emsg_invreg(var_start[1]); |
| 4622 | goto theend; |
| 4623 | } |
| 4624 | dest = dest_reg; |
| 4625 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4626 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4627 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4628 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4629 | { |
| 4630 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4631 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4632 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4633 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4634 | { |
| 4635 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4636 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4637 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4638 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4639 | { |
| 4640 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4641 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4642 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4643 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4644 | { |
| 4645 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4646 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4647 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4648 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4649 | { |
| 4650 | typval_T *vtv; |
| 4651 | int di_flags; |
| 4652 | |
| 4653 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4654 | if (vimvaridx < 0) |
| 4655 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4656 | semsg(_(e_variable_not_found_str), var_start); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4657 | goto theend; |
| 4658 | } |
| 4659 | // We use the current value of "sandbox" here, is that OK? |
| 4660 | if (var_check_ro(di_flags, name, FALSE)) |
| 4661 | goto theend; |
| 4662 | dest = dest_vimvar; |
| 4663 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4664 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4665 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4666 | } |
| 4667 | else |
| 4668 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4669 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4670 | |
| 4671 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4672 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4673 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4674 | semsg(_(e_cannot_use_reserved_name), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4675 | goto theend; |
| 4676 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4677 | |
| 4678 | lvar = lookup_local(var_start, varlen, cctx); |
| 4679 | if (lvar == NULL) |
| 4680 | { |
| 4681 | CLEAR_FIELD(arg_lvar); |
| 4682 | if (lookup_arg(var_start, varlen, |
| 4683 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4684 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4685 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4686 | if (is_decl) |
| 4687 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4688 | semsg(_(e_str_is_used_as_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4689 | goto theend; |
| 4690 | } |
| 4691 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4692 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4693 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4694 | if (lvar != NULL) |
| 4695 | { |
| 4696 | if (is_decl) |
| 4697 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4698 | semsg(_(e_variable_already_declared), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4699 | goto theend; |
| 4700 | } |
| 4701 | else if (lvar->lv_const) |
| 4702 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4703 | semsg(_(e_cannot_assign_to_constant), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4704 | goto theend; |
| 4705 | } |
| 4706 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4707 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4708 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4709 | int script_namespace = varlen > 1 |
| 4710 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4711 | int script_var = (script_namespace |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 4712 | ? lookup_script(var_start + 2, varlen - 2, FALSE) |
| 4713 | : lookup_script(var_start, varlen, TRUE)) == OK; |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4714 | imported_T *import = |
| 4715 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4716 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4717 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4718 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4719 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4720 | |
| 4721 | if (is_decl) |
| 4722 | { |
| 4723 | if (script_namespace) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4724 | semsg(_(e_cannot_declare_script_variable_in_function), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4725 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4726 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4727 | semsg(_(e_variable_already_declared_in_script), |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4728 | name); |
| 4729 | goto theend; |
| 4730 | } |
| 4731 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4732 | == SCRIPT_VERSION_VIM9 |
| 4733 | && script_namespace |
| 4734 | && !script_var && import == NULL) |
| 4735 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4736 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4737 | goto theend; |
| 4738 | } |
| 4739 | |
| 4740 | dest = dest_script; |
| 4741 | |
| 4742 | // existing script-local variables should have a type |
| 4743 | scriptvar_sid = current_sctx.sc_sid; |
| 4744 | if (import != NULL) |
| 4745 | scriptvar_sid = import->imp_sid; |
| 4746 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4747 | rawname, TRUE); |
| 4748 | if (scriptvar_idx >= 0) |
| 4749 | { |
| 4750 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4751 | svar_T *sv = |
| 4752 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4753 | + scriptvar_idx; |
| 4754 | type = sv->sv_type; |
| 4755 | } |
| 4756 | } |
| 4757 | else if (name[1] == ':' && name[2] != NUL) |
| 4758 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4759 | semsg(_(e_cannot_use_namespaced_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4760 | goto theend; |
| 4761 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4762 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4763 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4764 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4765 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4766 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4767 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4768 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4769 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4770 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4771 | |
| 4772 | if (declare_error) |
| 4773 | { |
| 4774 | vim9_declare_error(name); |
| 4775 | goto theend; |
| 4776 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | // handle "a:name" as a name, not index "name" on "a" |
| 4780 | if (varlen > 1 || var_start[varlen] != ':') |
| 4781 | p = var_end; |
| 4782 | |
| 4783 | if (dest != dest_option) |
| 4784 | { |
| 4785 | if (is_decl && *p == ':') |
| 4786 | { |
| 4787 | // parse optional type: "let var: type = expr" |
| 4788 | if (!VIM_ISWHITE(p[1])) |
| 4789 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4790 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4791 | goto theend; |
| 4792 | } |
| 4793 | p = skipwhite(p + 1); |
| 4794 | type = parse_type(&p, cctx->ctx_type_list); |
| 4795 | has_type = TRUE; |
| 4796 | } |
| 4797 | else if (lvar != NULL) |
| 4798 | type = lvar->lv_type; |
| 4799 | } |
| 4800 | |
| 4801 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4802 | && type->tt_type != VAR_STRING |
| 4803 | && type->tt_type != VAR_ANY) |
| 4804 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4805 | emsg(_(e_can_only_concatenate_to_string)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4806 | goto theend; |
| 4807 | } |
| 4808 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4809 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4810 | { |
| 4811 | if (oplen > 1 && !heredoc) |
| 4812 | { |
| 4813 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4814 | semsg(_(e_cannot_use_operator_on_new_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4815 | goto theend; |
| 4816 | } |
| 4817 | |
| 4818 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4819 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4820 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4821 | goto theend; |
| 4822 | lvar = reserve_local(cctx, var_start, varlen, |
| 4823 | cmdidx == CMD_const, type); |
| 4824 | if (lvar == NULL) |
| 4825 | goto theend; |
| 4826 | new_local = TRUE; |
| 4827 | } |
| 4828 | |
| 4829 | member_type = type; |
| 4830 | if (var_end > var_start + varlen) |
| 4831 | { |
| 4832 | // Something follows after the variable: "var[idx]". |
| 4833 | if (is_decl) |
| 4834 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4835 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4836 | goto theend; |
| 4837 | } |
| 4838 | |
| 4839 | if (var_start[varlen] == '[') |
| 4840 | { |
| 4841 | has_index = TRUE; |
| 4842 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4843 | member_type = &t_any; |
| 4844 | else |
| 4845 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4846 | } |
| 4847 | else |
| 4848 | { |
| 4849 | semsg("Not supported yet: %s", var_start); |
| 4850 | goto theend; |
| 4851 | } |
| 4852 | } |
| 4853 | else if (lvar == &arg_lvar) |
| 4854 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4855 | semsg(_(e_cannot_assign_to_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4856 | goto theend; |
| 4857 | } |
| 4858 | |
| 4859 | if (!heredoc) |
| 4860 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4861 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4862 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4863 | if (oplen > 0 && var_count == 0) |
| 4864 | { |
| 4865 | // skip over the "=" and the expression |
| 4866 | p = skipwhite(op + oplen); |
| 4867 | compile_expr0(&p, cctx); |
| 4868 | } |
| 4869 | } |
| 4870 | else if (oplen > 0) |
| 4871 | { |
| 4872 | type_T *stacktype; |
| 4873 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4874 | // For "var = expr" evaluate the expression. |
| 4875 | if (var_count == 0) |
| 4876 | { |
| 4877 | int r; |
| 4878 | |
| 4879 | // for "+=", "*=", "..=" etc. first load the current value |
| 4880 | if (*op != '=') |
| 4881 | { |
| 4882 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4883 | |
| 4884 | if (has_index) |
| 4885 | { |
| 4886 | // TODO: get member from list or dict |
| 4887 | emsg("Index with operation not supported yet"); |
| 4888 | goto theend; |
| 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | // Compile the expression. Temporarily hide the new local |
| 4893 | // variable here, it is not available to this expression. |
| 4894 | if (new_local) |
| 4895 | --cctx->ctx_locals.ga_len; |
| 4896 | instr_count = instr->ga_len; |
| 4897 | p = skipwhite(op + oplen); |
| 4898 | r = compile_expr0(&p, cctx); |
| 4899 | if (new_local) |
| 4900 | ++cctx->ctx_locals.ga_len; |
| 4901 | if (r == FAIL) |
| 4902 | goto theend; |
| 4903 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4904 | else if (semicolon && var_idx == var_count - 1) |
| 4905 | { |
| 4906 | // For "[var; var] = expr" get the rest of the list |
| 4907 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 4908 | goto theend; |
| 4909 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4910 | else |
| 4911 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4912 | // For "[var, var] = expr" get the "var_idx" item from the |
| 4913 | // list. |
| 4914 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 4915 | return FAIL; |
| 4916 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4917 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4918 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 4919 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4920 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4921 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4922 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4923 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4924 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4925 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4926 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4927 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4928 | } |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4929 | else if ((stacktype->tt_type == VAR_FUNC |
| 4930 | || stacktype->tt_type == VAR_PARTIAL) |
| 4931 | && var_wrong_func_name(name, TRUE)) |
| 4932 | { |
| 4933 | goto theend; |
| 4934 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4935 | else |
| 4936 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4937 | // An empty list or dict has a &t_void member, |
| 4938 | // for a variable that implies &t_any. |
| 4939 | if (stacktype == &t_list_empty) |
| 4940 | lvar->lv_type = &t_list_any; |
| 4941 | else if (stacktype == &t_dict_empty) |
| 4942 | lvar->lv_type = &t_dict_any; |
| 4943 | else |
| 4944 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4945 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4946 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 4947 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4948 | { |
| 4949 | type_T *use_type = lvar->lv_type; |
| 4950 | |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 4951 | // without operator type is here, otherwise below |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4952 | if (has_index) |
| 4953 | { |
| 4954 | use_type = use_type->tt_member; |
| 4955 | if (use_type == NULL) |
| 4956 | use_type = &t_void; |
| 4957 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4958 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 4959 | == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4960 | goto theend; |
| 4961 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4962 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4963 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4964 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4965 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4966 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4967 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4968 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4969 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4970 | goto theend; |
| 4971 | } |
| 4972 | else if (!has_type || dest == dest_option) |
| 4973 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4974 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4975 | goto theend; |
| 4976 | } |
| 4977 | else |
| 4978 | { |
| 4979 | // variables are always initialized |
| 4980 | if (ga_grow(instr, 1) == FAIL) |
| 4981 | goto theend; |
| 4982 | switch (member_type->tt_type) |
| 4983 | { |
| 4984 | case VAR_BOOL: |
| 4985 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 4986 | break; |
| 4987 | case VAR_FLOAT: |
| 4988 | #ifdef FEAT_FLOAT |
| 4989 | generate_PUSHF(cctx, 0.0); |
| 4990 | #endif |
| 4991 | break; |
| 4992 | case VAR_STRING: |
| 4993 | generate_PUSHS(cctx, NULL); |
| 4994 | break; |
| 4995 | case VAR_BLOB: |
| 4996 | generate_PUSHBLOB(cctx, NULL); |
| 4997 | break; |
| 4998 | case VAR_FUNC: |
| 4999 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5000 | break; |
| 5001 | case VAR_LIST: |
| 5002 | generate_NEWLIST(cctx, 0); |
| 5003 | break; |
| 5004 | case VAR_DICT: |
| 5005 | generate_NEWDICT(cctx, 0); |
| 5006 | break; |
| 5007 | case VAR_JOB: |
| 5008 | generate_PUSHJOB(cctx, NULL); |
| 5009 | break; |
| 5010 | case VAR_CHANNEL: |
| 5011 | generate_PUSHCHANNEL(cctx, NULL); |
| 5012 | break; |
| 5013 | case VAR_NUMBER: |
| 5014 | case VAR_UNKNOWN: |
| 5015 | case VAR_ANY: |
| 5016 | case VAR_PARTIAL: |
| 5017 | case VAR_VOID: |
| 5018 | case VAR_SPECIAL: // cannot happen |
| 5019 | generate_PUSHNR(cctx, 0); |
| 5020 | break; |
| 5021 | } |
| 5022 | } |
| 5023 | if (var_count == 0) |
| 5024 | end = p; |
| 5025 | } |
| 5026 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5027 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5028 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5029 | break; |
| 5030 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5031 | if (oplen > 0 && *op != '=') |
| 5032 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5033 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5034 | type_T *stacktype; |
| 5035 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5036 | if (*op == '.') |
| 5037 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5038 | else |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5039 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5040 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5041 | if ( |
| 5042 | #ifdef FEAT_FLOAT |
| 5043 | // If variable is float operation with number is OK. |
| 5044 | !(expected == &t_float && stacktype == &t_number) && |
| 5045 | #endif |
| 5046 | need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5047 | goto theend; |
| 5048 | |
| 5049 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5050 | { |
| 5051 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 5052 | goto theend; |
| 5053 | } |
| 5054 | else if (*op == '+') |
| 5055 | { |
| 5056 | if (generate_add_instr(cctx, |
| 5057 | operator_type(member_type, stacktype), |
| 5058 | member_type, stacktype) == FAIL) |
| 5059 | goto theend; |
| 5060 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5061 | else if (generate_two_op(cctx, op) == FAIL) |
| 5062 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5063 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5064 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5065 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5066 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5067 | int r; |
| 5068 | |
| 5069 | // Compile the "idx" in "var[idx]". |
| 5070 | if (new_local) |
| 5071 | --cctx->ctx_locals.ga_len; |
| 5072 | p = skipwhite(var_start + varlen + 1); |
| 5073 | r = compile_expr0(&p, cctx); |
| 5074 | if (new_local) |
| 5075 | ++cctx->ctx_locals.ga_len; |
| 5076 | if (r == FAIL) |
| 5077 | goto theend; |
| 5078 | if (*skipwhite(p) != ']') |
| 5079 | { |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 5080 | // this should not happen |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5081 | emsg(_(e_missbrac)); |
| 5082 | goto theend; |
| 5083 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5084 | if (type == &t_any) |
| 5085 | { |
| 5086 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5087 | stack->ga_len - 1]; |
| 5088 | // Index on variable of unknown type: guess the type from the |
| 5089 | // index type: number is dict, otherwise dict. |
| 5090 | // TODO: should do the assignment at runtime |
| 5091 | if (idx_type->tt_type == VAR_NUMBER) |
| 5092 | type = &t_list_any; |
| 5093 | else |
| 5094 | type = &t_dict_any; |
| 5095 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5096 | if (type->tt_type == VAR_DICT |
| 5097 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5098 | goto theend; |
| 5099 | if (type->tt_type == VAR_LIST |
| 5100 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5101 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5102 | { |
| 5103 | emsg(_(e_number_exp)); |
| 5104 | goto theend; |
| 5105 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5106 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5107 | // Load the dict or list. On the stack we then have: |
| 5108 | // - value |
| 5109 | // - index |
| 5110 | // - variable |
| 5111 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5112 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5113 | if (type->tt_type == VAR_LIST) |
| 5114 | { |
| 5115 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5116 | return FAIL; |
| 5117 | } |
| 5118 | else if (type->tt_type == VAR_DICT) |
| 5119 | { |
| 5120 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5121 | return FAIL; |
| 5122 | } |
| 5123 | else |
| 5124 | { |
| 5125 | emsg(_(e_listreq)); |
| 5126 | goto theend; |
| 5127 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5128 | } |
| 5129 | else |
| 5130 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5131 | switch (dest) |
| 5132 | { |
| 5133 | case dest_option: |
| 5134 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5135 | break; |
| 5136 | case dest_global: |
| 5137 | // include g: with the name, easier to execute that way |
| 5138 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5139 | break; |
| 5140 | case dest_buffer: |
| 5141 | // include b: with the name, easier to execute that way |
| 5142 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5143 | break; |
| 5144 | case dest_window: |
| 5145 | // include w: with the name, easier to execute that way |
| 5146 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5147 | break; |
| 5148 | case dest_tab: |
| 5149 | // include t: with the name, easier to execute that way |
| 5150 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5151 | break; |
| 5152 | case dest_env: |
| 5153 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5154 | break; |
| 5155 | case dest_reg: |
| 5156 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5157 | break; |
| 5158 | case dest_vimvar: |
| 5159 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5160 | break; |
| 5161 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5162 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5163 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5164 | { |
| 5165 | char_u *name_s = name; |
| 5166 | |
| 5167 | // Include s: in the name for store_var() |
| 5168 | if (name[1] != ':') |
| 5169 | { |
| 5170 | int len = (int)STRLEN(name) + 3; |
| 5171 | |
| 5172 | name_s = alloc(len); |
| 5173 | if (name_s == NULL) |
| 5174 | name_s = name; |
| 5175 | else |
| 5176 | vim_snprintf((char *)name_s, len, |
| 5177 | "s:%s", name); |
| 5178 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5179 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5180 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5181 | if (name_s != name) |
| 5182 | vim_free(name_s); |
| 5183 | } |
| 5184 | else |
| 5185 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5186 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5187 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5188 | break; |
| 5189 | case dest_local: |
| 5190 | if (lvar != NULL) |
| 5191 | { |
| 5192 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5193 | + instr->ga_len - 1; |
| 5194 | |
| 5195 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5196 | // ISN_STORE into ISN_STORENR |
| 5197 | if (!lvar->lv_from_outer |
| 5198 | && instr->ga_len == instr_count + 1 |
| 5199 | && isn->isn_type == ISN_PUSHNR) |
| 5200 | { |
| 5201 | varnumber_T val = isn->isn_arg.number; |
| 5202 | |
| 5203 | isn->isn_type = ISN_STORENR; |
| 5204 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5205 | isn->isn_arg.storenr.stnr_val = val; |
| 5206 | if (stack->ga_len > 0) |
| 5207 | --stack->ga_len; |
| 5208 | } |
| 5209 | else if (lvar->lv_from_outer) |
| 5210 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5211 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5212 | else |
| 5213 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5214 | } |
| 5215 | break; |
| 5216 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5217 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5218 | |
| 5219 | if (var_idx + 1 < var_count) |
| 5220 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5221 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5222 | |
| 5223 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5224 | if (var_count > 0 && !semicolon) |
| 5225 | { |
| 5226 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5227 | goto theend; |
| 5228 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5229 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5230 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5231 | |
| 5232 | theend: |
| 5233 | vim_free(name); |
| 5234 | return ret; |
| 5235 | } |
| 5236 | |
| 5237 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5238 | * Check if "name" can be "unlet". |
| 5239 | */ |
| 5240 | int |
| 5241 | check_vim9_unlet(char_u *name) |
| 5242 | { |
| 5243 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5244 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 5245 | // "unlet s:var" is allowed in legacy script. |
| 5246 | if (*name == 's' && !script_is_vim9()) |
| 5247 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5248 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5249 | return FAIL; |
| 5250 | } |
| 5251 | return OK; |
| 5252 | } |
| 5253 | |
| 5254 | /* |
| 5255 | * Callback passed to ex_unletlock(). |
| 5256 | */ |
| 5257 | static int |
| 5258 | compile_unlet( |
| 5259 | lval_T *lvp, |
| 5260 | char_u *name_end, |
| 5261 | exarg_T *eap, |
| 5262 | int deep UNUSED, |
| 5263 | void *coookie) |
| 5264 | { |
| 5265 | cctx_T *cctx = coookie; |
| 5266 | |
| 5267 | if (lvp->ll_tv == NULL) |
| 5268 | { |
| 5269 | char_u *p = lvp->ll_name; |
| 5270 | int cc = *name_end; |
| 5271 | int ret = OK; |
| 5272 | |
| 5273 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5274 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5275 | if (*p == '$') |
| 5276 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5277 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5278 | ret = FAIL; |
| 5279 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5280 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5281 | |
| 5282 | *name_end = cc; |
| 5283 | return ret; |
| 5284 | } |
| 5285 | |
| 5286 | // TODO: unlet {list}[idx] |
| 5287 | // TODO: unlet {dict}[key] |
| 5288 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5289 | return FAIL; |
| 5290 | } |
| 5291 | |
| 5292 | /* |
| 5293 | * compile "unlet var", "lock var" and "unlock var" |
| 5294 | * "arg" points to "var". |
| 5295 | */ |
| 5296 | static char_u * |
| 5297 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5298 | { |
| 5299 | char_u *p = arg; |
| 5300 | |
| 5301 | if (eap->cmdidx != CMD_unlet) |
| 5302 | { |
| 5303 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | |
| 5307 | if (*p == '!') |
| 5308 | { |
| 5309 | p = skipwhite(p + 1); |
| 5310 | eap->forceit = TRUE; |
| 5311 | } |
| 5312 | |
| 5313 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5314 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5315 | } |
| 5316 | |
| 5317 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5318 | * Compile an :import command. |
| 5319 | */ |
| 5320 | static char_u * |
| 5321 | compile_import(char_u *arg, cctx_T *cctx) |
| 5322 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5323 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | /* |
| 5327 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5328 | */ |
| 5329 | static int |
| 5330 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5331 | { |
| 5332 | garray_T *instr = &cctx->ctx_instr; |
| 5333 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5334 | |
| 5335 | if (endlabel == NULL) |
| 5336 | return FAIL; |
| 5337 | endlabel->el_next = *el; |
| 5338 | *el = endlabel; |
| 5339 | endlabel->el_end_label = instr->ga_len; |
| 5340 | |
| 5341 | generate_JUMP(cctx, when, 0); |
| 5342 | return OK; |
| 5343 | } |
| 5344 | |
| 5345 | static void |
| 5346 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5347 | { |
| 5348 | garray_T *instr = &cctx->ctx_instr; |
| 5349 | |
| 5350 | while (*el != NULL) |
| 5351 | { |
| 5352 | endlabel_T *cur = (*el); |
| 5353 | isn_T *isn; |
| 5354 | |
| 5355 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5356 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5357 | *el = cur->el_next; |
| 5358 | vim_free(cur); |
| 5359 | } |
| 5360 | } |
| 5361 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5362 | static void |
| 5363 | compile_free_jump_to_end(endlabel_T **el) |
| 5364 | { |
| 5365 | while (*el != NULL) |
| 5366 | { |
| 5367 | endlabel_T *cur = (*el); |
| 5368 | |
| 5369 | *el = cur->el_next; |
| 5370 | vim_free(cur); |
| 5371 | } |
| 5372 | } |
| 5373 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5374 | /* |
| 5375 | * Create a new scope and set up the generic items. |
| 5376 | */ |
| 5377 | static scope_T * |
| 5378 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5379 | { |
| 5380 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5381 | |
| 5382 | if (scope == NULL) |
| 5383 | return NULL; |
| 5384 | scope->se_outer = cctx->ctx_scope; |
| 5385 | cctx->ctx_scope = scope; |
| 5386 | scope->se_type = type; |
| 5387 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5388 | return scope; |
| 5389 | } |
| 5390 | |
| 5391 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5392 | * Free the current scope and go back to the outer scope. |
| 5393 | */ |
| 5394 | static void |
| 5395 | drop_scope(cctx_T *cctx) |
| 5396 | { |
| 5397 | scope_T *scope = cctx->ctx_scope; |
| 5398 | |
| 5399 | if (scope == NULL) |
| 5400 | { |
| 5401 | iemsg("calling drop_scope() without a scope"); |
| 5402 | return; |
| 5403 | } |
| 5404 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5405 | switch (scope->se_type) |
| 5406 | { |
| 5407 | case IF_SCOPE: |
| 5408 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5409 | case FOR_SCOPE: |
| 5410 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5411 | case WHILE_SCOPE: |
| 5412 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5413 | case TRY_SCOPE: |
| 5414 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5415 | case NO_SCOPE: |
| 5416 | case BLOCK_SCOPE: |
| 5417 | break; |
| 5418 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5419 | vim_free(scope); |
| 5420 | } |
| 5421 | |
| 5422 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5423 | * compile "if expr" |
| 5424 | * |
| 5425 | * "if expr" Produces instructions: |
| 5426 | * EVAL expr Push result of "expr" |
| 5427 | * JUMP_IF_FALSE end |
| 5428 | * ... body ... |
| 5429 | * end: |
| 5430 | * |
| 5431 | * "if expr | else" Produces instructions: |
| 5432 | * EVAL expr Push result of "expr" |
| 5433 | * JUMP_IF_FALSE else |
| 5434 | * ... body ... |
| 5435 | * JUMP_ALWAYS end |
| 5436 | * else: |
| 5437 | * ... body ... |
| 5438 | * end: |
| 5439 | * |
| 5440 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5441 | * EVAL expr Push result of "expr" |
| 5442 | * JUMP_IF_FALSE elseif |
| 5443 | * ... body ... |
| 5444 | * JUMP_ALWAYS end |
| 5445 | * elseif: |
| 5446 | * EVAL expr Push result of "expr" |
| 5447 | * JUMP_IF_FALSE else |
| 5448 | * ... body ... |
| 5449 | * JUMP_ALWAYS end |
| 5450 | * else: |
| 5451 | * ... body ... |
| 5452 | * end: |
| 5453 | */ |
| 5454 | static char_u * |
| 5455 | compile_if(char_u *arg, cctx_T *cctx) |
| 5456 | { |
| 5457 | char_u *p = arg; |
| 5458 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5459 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5460 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5461 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5462 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5463 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5464 | CLEAR_FIELD(ppconst); |
| 5465 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5466 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5467 | clear_ppconst(&ppconst); |
| 5468 | return NULL; |
| 5469 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5470 | if (cctx->ctx_skip == SKIP_YES) |
| 5471 | clear_ppconst(&ppconst); |
| 5472 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5473 | { |
| 5474 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5475 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5476 | clear_ppconst(&ppconst); |
| 5477 | } |
| 5478 | else |
| 5479 | { |
| 5480 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5481 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5482 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5483 | return NULL; |
| 5484 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5485 | |
| 5486 | scope = new_scope(cctx, IF_SCOPE); |
| 5487 | if (scope == NULL) |
| 5488 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5489 | scope->se_skip_save = skip_save; |
| 5490 | // "is_had_return" will be reset if any block does not end in :return |
| 5491 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5492 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5493 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5494 | { |
| 5495 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5496 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5497 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5498 | } |
| 5499 | else |
| 5500 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5501 | |
| 5502 | return p; |
| 5503 | } |
| 5504 | |
| 5505 | static char_u * |
| 5506 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5507 | { |
| 5508 | char_u *p = arg; |
| 5509 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5510 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5511 | isn_T *isn; |
| 5512 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5513 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5514 | |
| 5515 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5516 | { |
| 5517 | emsg(_(e_elseif_without_if)); |
| 5518 | return NULL; |
| 5519 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5520 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5521 | if (!cctx->ctx_had_return) |
| 5522 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5523 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5524 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5525 | { |
| 5526 | 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] | 5527 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5528 | return NULL; |
| 5529 | // previous "if" or "elseif" jumps here |
| 5530 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5531 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5532 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5533 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5534 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5535 | CLEAR_FIELD(ppconst); |
| 5536 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5537 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5538 | clear_ppconst(&ppconst); |
| 5539 | return NULL; |
| 5540 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5541 | if (scope->se_skip_save == SKIP_YES) |
| 5542 | clear_ppconst(&ppconst); |
| 5543 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5544 | { |
| 5545 | // The expression results in a constant. |
| 5546 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5547 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5548 | clear_ppconst(&ppconst); |
| 5549 | scope->se_u.se_if.is_if_label = -1; |
| 5550 | } |
| 5551 | else |
| 5552 | { |
| 5553 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5554 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5555 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5556 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5557 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5558 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5559 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5560 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5561 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5562 | |
| 5563 | return p; |
| 5564 | } |
| 5565 | |
| 5566 | static char_u * |
| 5567 | compile_else(char_u *arg, cctx_T *cctx) |
| 5568 | { |
| 5569 | char_u *p = arg; |
| 5570 | garray_T *instr = &cctx->ctx_instr; |
| 5571 | isn_T *isn; |
| 5572 | scope_T *scope = cctx->ctx_scope; |
| 5573 | |
| 5574 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5575 | { |
| 5576 | emsg(_(e_else_without_if)); |
| 5577 | return NULL; |
| 5578 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5579 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5580 | if (!cctx->ctx_had_return) |
| 5581 | scope->se_u.se_if.is_had_return = FALSE; |
| 5582 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5583 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5584 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5585 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5586 | // jump from previous block to the end, unless the else block is empty |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5587 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5588 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5589 | if (!cctx->ctx_had_return |
| 5590 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5591 | JUMP_ALWAYS, cctx) == FAIL) |
| 5592 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5593 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5594 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5595 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5596 | { |
| 5597 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5598 | { |
| 5599 | // previous "if" or "elseif" jumps here |
| 5600 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5601 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5602 | scope->se_u.se_if.is_if_label = -1; |
| 5603 | } |
| 5604 | } |
| 5605 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5606 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5607 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5608 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5609 | |
| 5610 | return p; |
| 5611 | } |
| 5612 | |
| 5613 | static char_u * |
| 5614 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5615 | { |
| 5616 | scope_T *scope = cctx->ctx_scope; |
| 5617 | ifscope_T *ifscope; |
| 5618 | garray_T *instr = &cctx->ctx_instr; |
| 5619 | isn_T *isn; |
| 5620 | |
| 5621 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5622 | { |
| 5623 | emsg(_(e_endif_without_if)); |
| 5624 | return NULL; |
| 5625 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5626 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5627 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5628 | if (!cctx->ctx_had_return) |
| 5629 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5630 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5631 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5632 | { |
| 5633 | // previous "if" or "elseif" jumps here |
| 5634 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5635 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5636 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5637 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5638 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5639 | cctx->ctx_skip = scope->se_skip_save; |
| 5640 | |
| 5641 | // If all the blocks end in :return and there is an :else then the |
| 5642 | // had_return flag is set. |
| 5643 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5644 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5645 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5646 | return arg; |
| 5647 | } |
| 5648 | |
| 5649 | /* |
| 5650 | * compile "for var in expr" |
| 5651 | * |
| 5652 | * Produces instructions: |
| 5653 | * PUSHNR -1 |
| 5654 | * STORE loop-idx Set index to -1 |
| 5655 | * EVAL expr Push result of "expr" |
| 5656 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5657 | * - if beyond end, jump to "end" |
| 5658 | * - otherwise get item from list and push it |
| 5659 | * STORE var Store item in "var" |
| 5660 | * ... body ... |
| 5661 | * JUMP top Jump back to repeat |
| 5662 | * end: DROP Drop the result of "expr" |
| 5663 | * |
| 5664 | */ |
| 5665 | static char_u * |
| 5666 | compile_for(char_u *arg, cctx_T *cctx) |
| 5667 | { |
| 5668 | char_u *p; |
| 5669 | size_t varlen; |
| 5670 | garray_T *instr = &cctx->ctx_instr; |
| 5671 | garray_T *stack = &cctx->ctx_type_stack; |
| 5672 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5673 | lvar_T *loop_lvar; // loop iteration variable |
| 5674 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5675 | type_T *vartype; |
| 5676 | |
| 5677 | // TODO: list of variables: "for [key, value] in dict" |
| 5678 | // parse "var" |
| 5679 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5680 | ; |
| 5681 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5682 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5683 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5684 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5685 | semsg(_(e_variable_already_declared), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5686 | return NULL; |
| 5687 | } |
| 5688 | |
| 5689 | // consume "in" |
| 5690 | p = skipwhite(p); |
| 5691 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5692 | { |
| 5693 | emsg(_(e_missing_in)); |
| 5694 | return NULL; |
| 5695 | } |
| 5696 | p = skipwhite(p + 2); |
| 5697 | |
| 5698 | |
| 5699 | scope = new_scope(cctx, FOR_SCOPE); |
| 5700 | if (scope == NULL) |
| 5701 | return NULL; |
| 5702 | |
| 5703 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5704 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5705 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5706 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5707 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5708 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5709 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5710 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5711 | |
| 5712 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5713 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5714 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5715 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5716 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5717 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5718 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5719 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5720 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5721 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5722 | |
| 5723 | // compile "expr", it remains on the stack until "endfor" |
| 5724 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5725 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5726 | { |
| 5727 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5728 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5729 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5730 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5731 | // Now that we know the type of "var", check that it is a list, now or at |
| 5732 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5733 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5734 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5735 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5736 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5737 | return NULL; |
| 5738 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5739 | if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5740 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5741 | |
| 5742 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5743 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5744 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5745 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5746 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5747 | |
| 5748 | return arg; |
| 5749 | } |
| 5750 | |
| 5751 | /* |
| 5752 | * compile "endfor" |
| 5753 | */ |
| 5754 | static char_u * |
| 5755 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5756 | { |
| 5757 | garray_T *instr = &cctx->ctx_instr; |
| 5758 | scope_T *scope = cctx->ctx_scope; |
| 5759 | forscope_T *forscope; |
| 5760 | isn_T *isn; |
| 5761 | |
| 5762 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5763 | { |
| 5764 | emsg(_(e_for)); |
| 5765 | return NULL; |
| 5766 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5767 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5768 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5769 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5770 | |
| 5771 | // At end of ":for" scope jump back to the FOR instruction. |
| 5772 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5773 | |
| 5774 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5775 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5776 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5777 | |
| 5778 | // Fill in the "end" label any BREAK statements |
| 5779 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5780 | |
| 5781 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5782 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5783 | return NULL; |
| 5784 | |
| 5785 | vim_free(scope); |
| 5786 | |
| 5787 | return arg; |
| 5788 | } |
| 5789 | |
| 5790 | /* |
| 5791 | * compile "while expr" |
| 5792 | * |
| 5793 | * Produces instructions: |
| 5794 | * top: EVAL expr Push result of "expr" |
| 5795 | * JUMP_IF_FALSE end jump if false |
| 5796 | * ... body ... |
| 5797 | * JUMP top Jump back to repeat |
| 5798 | * end: |
| 5799 | * |
| 5800 | */ |
| 5801 | static char_u * |
| 5802 | compile_while(char_u *arg, cctx_T *cctx) |
| 5803 | { |
| 5804 | char_u *p = arg; |
| 5805 | garray_T *instr = &cctx->ctx_instr; |
| 5806 | scope_T *scope; |
| 5807 | |
| 5808 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5809 | if (scope == NULL) |
| 5810 | return NULL; |
| 5811 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5812 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5813 | |
| 5814 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5815 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5816 | return NULL; |
| 5817 | |
| 5818 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5819 | 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] | 5820 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5821 | return FAIL; |
| 5822 | |
| 5823 | return p; |
| 5824 | } |
| 5825 | |
| 5826 | /* |
| 5827 | * compile "endwhile" |
| 5828 | */ |
| 5829 | static char_u * |
| 5830 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5831 | { |
| 5832 | scope_T *scope = cctx->ctx_scope; |
| 5833 | |
| 5834 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5835 | { |
| 5836 | emsg(_(e_while)); |
| 5837 | return NULL; |
| 5838 | } |
| 5839 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5840 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5841 | |
| 5842 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5843 | 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] | 5844 | |
| 5845 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5846 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5847 | 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] | 5848 | |
| 5849 | vim_free(scope); |
| 5850 | |
| 5851 | return arg; |
| 5852 | } |
| 5853 | |
| 5854 | /* |
| 5855 | * compile "continue" |
| 5856 | */ |
| 5857 | static char_u * |
| 5858 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5859 | { |
| 5860 | scope_T *scope = cctx->ctx_scope; |
| 5861 | |
| 5862 | for (;;) |
| 5863 | { |
| 5864 | if (scope == NULL) |
| 5865 | { |
| 5866 | emsg(_(e_continue)); |
| 5867 | return NULL; |
| 5868 | } |
| 5869 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5870 | break; |
| 5871 | scope = scope->se_outer; |
| 5872 | } |
| 5873 | |
| 5874 | // Jump back to the FOR or WHILE instruction. |
| 5875 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5876 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5877 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5878 | return arg; |
| 5879 | } |
| 5880 | |
| 5881 | /* |
| 5882 | * compile "break" |
| 5883 | */ |
| 5884 | static char_u * |
| 5885 | compile_break(char_u *arg, cctx_T *cctx) |
| 5886 | { |
| 5887 | scope_T *scope = cctx->ctx_scope; |
| 5888 | endlabel_T **el; |
| 5889 | |
| 5890 | for (;;) |
| 5891 | { |
| 5892 | if (scope == NULL) |
| 5893 | { |
| 5894 | emsg(_(e_break)); |
| 5895 | return NULL; |
| 5896 | } |
| 5897 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5898 | break; |
| 5899 | scope = scope->se_outer; |
| 5900 | } |
| 5901 | |
| 5902 | // Jump to the end of the FOR or WHILE loop. |
| 5903 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5904 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5905 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5906 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5907 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5908 | return FAIL; |
| 5909 | |
| 5910 | return arg; |
| 5911 | } |
| 5912 | |
| 5913 | /* |
| 5914 | * compile "{" start of block |
| 5915 | */ |
| 5916 | static char_u * |
| 5917 | compile_block(char_u *arg, cctx_T *cctx) |
| 5918 | { |
| 5919 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5920 | return NULL; |
| 5921 | return skipwhite(arg + 1); |
| 5922 | } |
| 5923 | |
| 5924 | /* |
| 5925 | * compile end of block: drop one scope |
| 5926 | */ |
| 5927 | static void |
| 5928 | compile_endblock(cctx_T *cctx) |
| 5929 | { |
| 5930 | scope_T *scope = cctx->ctx_scope; |
| 5931 | |
| 5932 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5933 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5934 | vim_free(scope); |
| 5935 | } |
| 5936 | |
| 5937 | /* |
| 5938 | * compile "try" |
| 5939 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5940 | * finally. |
| 5941 | * Creates another scope for the "try" block itself. |
| 5942 | * TRY instruction sets up exception handling at runtime. |
| 5943 | * |
| 5944 | * "try" |
| 5945 | * TRY -> catch1, -> finally push trystack entry |
| 5946 | * ... try block |
| 5947 | * "throw {exception}" |
| 5948 | * EVAL {exception} |
| 5949 | * THROW create exception |
| 5950 | * ... try block |
| 5951 | * " catch {expr}" |
| 5952 | * JUMP -> finally |
| 5953 | * catch1: PUSH exeception |
| 5954 | * EVAL {expr} |
| 5955 | * MATCH |
| 5956 | * JUMP nomatch -> catch2 |
| 5957 | * CATCH remove exception |
| 5958 | * ... catch block |
| 5959 | * " catch" |
| 5960 | * JUMP -> finally |
| 5961 | * catch2: CATCH remove exception |
| 5962 | * ... catch block |
| 5963 | * " finally" |
| 5964 | * finally: |
| 5965 | * ... finally block |
| 5966 | * " endtry" |
| 5967 | * ENDTRY pop trystack entry, may rethrow |
| 5968 | */ |
| 5969 | static char_u * |
| 5970 | compile_try(char_u *arg, cctx_T *cctx) |
| 5971 | { |
| 5972 | garray_T *instr = &cctx->ctx_instr; |
| 5973 | scope_T *try_scope; |
| 5974 | scope_T *scope; |
| 5975 | |
| 5976 | // scope that holds the jumps that go to catch/finally/endtry |
| 5977 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5978 | if (try_scope == NULL) |
| 5979 | return NULL; |
| 5980 | |
| 5981 | // "catch" is set when the first ":catch" is found. |
| 5982 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5983 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5984 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5985 | return NULL; |
| 5986 | |
| 5987 | // scope for the try block itself |
| 5988 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5989 | if (scope == NULL) |
| 5990 | return NULL; |
| 5991 | |
| 5992 | return arg; |
| 5993 | } |
| 5994 | |
| 5995 | /* |
| 5996 | * compile "catch {expr}" |
| 5997 | */ |
| 5998 | static char_u * |
| 5999 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6000 | { |
| 6001 | scope_T *scope = cctx->ctx_scope; |
| 6002 | garray_T *instr = &cctx->ctx_instr; |
| 6003 | char_u *p; |
| 6004 | isn_T *isn; |
| 6005 | |
| 6006 | // end block scope from :try or :catch |
| 6007 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6008 | compile_endblock(cctx); |
| 6009 | scope = cctx->ctx_scope; |
| 6010 | |
| 6011 | // Error if not in a :try scope |
| 6012 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6013 | { |
| 6014 | emsg(_(e_catch)); |
| 6015 | return NULL; |
| 6016 | } |
| 6017 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6018 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6019 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6020 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6021 | return NULL; |
| 6022 | } |
| 6023 | |
| 6024 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6025 | 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] | 6026 | JUMP_ALWAYS, cctx) == FAIL) |
| 6027 | return NULL; |
| 6028 | |
| 6029 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6030 | 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] | 6031 | if (isn->isn_arg.try.try_catch == 0) |
| 6032 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6033 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6034 | { |
| 6035 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6036 | 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] | 6037 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6038 | } |
| 6039 | |
| 6040 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6041 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6042 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6043 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6044 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6045 | } |
| 6046 | else |
| 6047 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6048 | char_u *end; |
| 6049 | char_u *pat; |
| 6050 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6051 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6052 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6053 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6054 | // Push v:exception, push {expr} and MATCH |
| 6055 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6056 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6057 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6058 | if (*end != *p) |
| 6059 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 6060 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6061 | vim_free(tofree); |
| 6062 | return FAIL; |
| 6063 | } |
| 6064 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6065 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6066 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6067 | len = (int)(end - tofree); |
| 6068 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6069 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6070 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6071 | if (pat == NULL) |
| 6072 | return FAIL; |
| 6073 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6074 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6075 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6076 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6077 | return NULL; |
| 6078 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6079 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6080 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6081 | return NULL; |
| 6082 | } |
| 6083 | |
| 6084 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6085 | return NULL; |
| 6086 | |
| 6087 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6088 | return NULL; |
| 6089 | return p; |
| 6090 | } |
| 6091 | |
| 6092 | static char_u * |
| 6093 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6094 | { |
| 6095 | scope_T *scope = cctx->ctx_scope; |
| 6096 | garray_T *instr = &cctx->ctx_instr; |
| 6097 | isn_T *isn; |
| 6098 | |
| 6099 | // end block scope from :try or :catch |
| 6100 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6101 | compile_endblock(cctx); |
| 6102 | scope = cctx->ctx_scope; |
| 6103 | |
| 6104 | // Error if not in a :try scope |
| 6105 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6106 | { |
| 6107 | emsg(_(e_finally)); |
| 6108 | return NULL; |
| 6109 | } |
| 6110 | |
| 6111 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6112 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6113 | if (isn->isn_arg.try.try_finally != 0) |
| 6114 | { |
| 6115 | emsg(_(e_finally_dup)); |
| 6116 | return NULL; |
| 6117 | } |
| 6118 | |
| 6119 | // 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] | 6120 | 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] | 6121 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6122 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6123 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6124 | { |
| 6125 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6126 | 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] | 6127 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6128 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6129 | } |
| 6130 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6131 | // TODO: set index in ts_finally_label jumps |
| 6132 | |
| 6133 | return arg; |
| 6134 | } |
| 6135 | |
| 6136 | static char_u * |
| 6137 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6138 | { |
| 6139 | scope_T *scope = cctx->ctx_scope; |
| 6140 | garray_T *instr = &cctx->ctx_instr; |
| 6141 | isn_T *isn; |
| 6142 | |
| 6143 | // end block scope from :catch or :finally |
| 6144 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6145 | compile_endblock(cctx); |
| 6146 | scope = cctx->ctx_scope; |
| 6147 | |
| 6148 | // Error if not in a :try scope |
| 6149 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6150 | { |
| 6151 | if (scope == NULL) |
| 6152 | emsg(_(e_no_endtry)); |
| 6153 | else if (scope->se_type == WHILE_SCOPE) |
| 6154 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6155 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6156 | emsg(_(e_endfor)); |
| 6157 | else |
| 6158 | emsg(_(e_endif)); |
| 6159 | return NULL; |
| 6160 | } |
| 6161 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6162 | 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] | 6163 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6164 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6165 | emsg(_(e_missing_catch_or_finally)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6166 | return NULL; |
| 6167 | } |
| 6168 | |
| 6169 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6170 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6171 | 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] | 6172 | |
| 6173 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6174 | if (isn->isn_arg.try.try_catch == 0) |
| 6175 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6176 | if (isn->isn_arg.try.try_finally == 0) |
| 6177 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6178 | |
| 6179 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6180 | { |
| 6181 | // Last catch without match jumps here |
| 6182 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6183 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6184 | } |
| 6185 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6186 | compile_endblock(cctx); |
| 6187 | |
| 6188 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6189 | return NULL; |
| 6190 | return arg; |
| 6191 | } |
| 6192 | |
| 6193 | /* |
| 6194 | * compile "throw {expr}" |
| 6195 | */ |
| 6196 | static char_u * |
| 6197 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6198 | { |
| 6199 | char_u *p = skipwhite(arg); |
| 6200 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6201 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6202 | return NULL; |
| 6203 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6204 | return NULL; |
| 6205 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6206 | return NULL; |
| 6207 | |
| 6208 | return p; |
| 6209 | } |
| 6210 | |
| 6211 | /* |
| 6212 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6213 | * compile "echomsg expr" |
| 6214 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6215 | * compile "execute expr" |
| 6216 | */ |
| 6217 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6218 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6219 | { |
| 6220 | char_u *p = arg; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6221 | char_u *prev; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6222 | int count = 0; |
| 6223 | |
| 6224 | for (;;) |
| 6225 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6226 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6227 | return NULL; |
| 6228 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6229 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6230 | p = skipwhite(p); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6231 | if (ends_excmd2(prev, p)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6232 | break; |
| 6233 | } |
| 6234 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6235 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6236 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6237 | else if (cmdidx == CMD_execute) |
| 6238 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6239 | else if (cmdidx == CMD_echomsg) |
| 6240 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6241 | else |
| 6242 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6243 | return p; |
| 6244 | } |
| 6245 | |
| 6246 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6247 | * A command that is not compiled, execute with legacy code. |
| 6248 | */ |
| 6249 | static char_u * |
| 6250 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6251 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6252 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6253 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6254 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6255 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6256 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6257 | goto theend; |
| 6258 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6259 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6260 | { |
| 6261 | long argt = excmd_get_argt(eap->cmdidx); |
| 6262 | int usefilter = FALSE; |
| 6263 | |
| 6264 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6265 | |
| 6266 | // If the command can be followed by a bar, find the bar and truncate |
| 6267 | // it, so that the following command can be compiled. |
| 6268 | // The '|' is overwritten with a NUL, it is put back below. |
| 6269 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6270 | && *eap->arg == '!') |
| 6271 | // :w !filter or :r !filter or :r! filter |
| 6272 | usefilter = TRUE; |
| 6273 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6274 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 6275 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6276 | separate_nextcmd(eap); |
| 6277 | if (eap->nextcmd != NULL) |
| 6278 | nextcmd = eap->nextcmd; |
| 6279 | } |
| 6280 | } |
| 6281 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6282 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6283 | { |
| 6284 | // expand filename in "syntax include [@group] filename" |
| 6285 | has_expr = TRUE; |
| 6286 | eap->arg = skipwhite(eap->arg + 7); |
| 6287 | if (*eap->arg == '@') |
| 6288 | eap->arg = skiptowhite(eap->arg); |
| 6289 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6290 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6291 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6292 | { |
| 6293 | int count = 0; |
| 6294 | char_u *start = skipwhite(line); |
| 6295 | |
| 6296 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6297 | // PUSHS ":cmd xxx" |
| 6298 | // eval expr1 |
| 6299 | // PUSHS "yyy" |
| 6300 | // eval expr2 |
| 6301 | // PUSHS "zzz" |
| 6302 | // EXECCONCAT 5 |
| 6303 | for (;;) |
| 6304 | { |
| 6305 | if (p > start) |
| 6306 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6307 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6308 | ++count; |
| 6309 | } |
| 6310 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6311 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6312 | return NULL; |
| 6313 | may_generate_2STRING(-1, cctx); |
| 6314 | ++count; |
| 6315 | p = skipwhite(p); |
| 6316 | if (*p != '`') |
| 6317 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6318 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6319 | return NULL; |
| 6320 | } |
| 6321 | start = p + 1; |
| 6322 | |
| 6323 | p = (char_u *)strstr((char *)start, "`="); |
| 6324 | if (p == NULL) |
| 6325 | { |
| 6326 | if (*skipwhite(start) != NUL) |
| 6327 | { |
| 6328 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6329 | ++count; |
| 6330 | } |
| 6331 | break; |
| 6332 | } |
| 6333 | } |
| 6334 | generate_EXECCONCAT(cctx, count); |
| 6335 | } |
| 6336 | else |
| 6337 | generate_EXEC(cctx, line); |
| 6338 | |
| 6339 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6340 | if (*nextcmd != NUL) |
| 6341 | { |
| 6342 | // the parser expects a pointer to the bar, put it back |
| 6343 | --nextcmd; |
| 6344 | *nextcmd = '|'; |
| 6345 | } |
| 6346 | |
| 6347 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6351 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6352 | * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet. |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6353 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6354 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6355 | add_def_function(ufunc_T *ufunc) |
| 6356 | { |
| 6357 | dfunc_T *dfunc; |
| 6358 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6359 | if (def_functions.ga_len == 0) |
| 6360 | { |
| 6361 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6362 | // wasn't set. |
| 6363 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6364 | return FAIL; |
| 6365 | ++def_functions.ga_len; |
| 6366 | } |
| 6367 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6368 | // Add the function to "def_functions". |
| 6369 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6370 | return FAIL; |
| 6371 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6372 | CLEAR_POINTER(dfunc); |
| 6373 | dfunc->df_idx = def_functions.ga_len; |
| 6374 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6375 | dfunc->df_ufunc = ufunc; |
| 6376 | ++def_functions.ga_len; |
| 6377 | return OK; |
| 6378 | } |
| 6379 | |
| 6380 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6381 | * After ex_function() has collected all the function lines: parse and compile |
| 6382 | * the lines into instructions. |
| 6383 | * Adds the function to "def_functions". |
| 6384 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6385 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6386 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6387 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6388 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6389 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6390 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6391 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6392 | 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] | 6393 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6394 | char_u *line = NULL; |
| 6395 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6396 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6397 | cctx_T cctx; |
| 6398 | garray_T *instr; |
| 6399 | int called_emsg_before = called_emsg; |
| 6400 | int ret = FAIL; |
| 6401 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6402 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6403 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6404 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6405 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6406 | // When using a function that was compiled before: Free old instructions. |
| 6407 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6408 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6409 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6410 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6411 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6412 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6413 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6414 | else |
| 6415 | { |
| 6416 | if (add_def_function(ufunc) == FAIL) |
| 6417 | return FAIL; |
| 6418 | new_def_function = TRUE; |
| 6419 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6420 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6421 | ufunc->uf_def_status = UF_COMPILING; |
| 6422 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6423 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6424 | cctx.ctx_ufunc = ufunc; |
| 6425 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6426 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6427 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6428 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6429 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6430 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6431 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6432 | instr = &cctx.ctx_instr; |
| 6433 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6434 | // Set the context to the function, it may be compiled when called from |
| 6435 | // another script. Set the script version to the most modern one. |
| 6436 | // The line number will be set in next_line_from_context(). |
| 6437 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6438 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6439 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6440 | // Make sure error messages are OK. |
| 6441 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6442 | if (do_estack_push) |
| 6443 | estack_push_ufunc(ufunc, 1); |
| 6444 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6445 | if (ufunc->uf_def_args.ga_len > 0) |
| 6446 | { |
| 6447 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6448 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6449 | int i; |
| 6450 | char_u *arg; |
| 6451 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6452 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6453 | |
| 6454 | // Produce instructions for the default values of optional arguments. |
| 6455 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6456 | // where to start when the function is called, depending on the number |
| 6457 | // of arguments. |
| 6458 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6459 | if (ufunc->uf_def_arg_idx == NULL) |
| 6460 | goto erret; |
| 6461 | for (i = 0; i < count; ++i) |
| 6462 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6463 | garray_T *stack = &cctx.ctx_type_stack; |
| 6464 | type_T *val_type; |
| 6465 | int arg_idx = first_def_arg + i; |
| 6466 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6467 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6468 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6469 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6470 | goto erret; |
| 6471 | |
| 6472 | // If no type specified use the type of the default value. |
| 6473 | // Otherwise check that the default value type matches the |
| 6474 | // specified type. |
| 6475 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6476 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6477 | { |
| 6478 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6479 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6480 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6481 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6482 | == FAIL) |
| 6483 | { |
| 6484 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6485 | arg_idx + 1); |
| 6486 | goto erret; |
| 6487 | } |
| 6488 | |
| 6489 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6490 | goto erret; |
| 6491 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6492 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6493 | |
| 6494 | if (did_set_arg_type) |
| 6495 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | /* |
| 6499 | * Loop over all the lines of the function and generate instructions. |
| 6500 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6501 | for (;;) |
| 6502 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6503 | exarg_T ea; |
| 6504 | cmdmod_T save_cmdmod; |
| 6505 | int starts_with_colon = FALSE; |
| 6506 | char_u *cmd; |
| 6507 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6508 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6509 | // Bail out on the first error to avoid a flood of errors and report |
| 6510 | // the right line number when inside try/catch. |
| 6511 | if (emsg_before != called_emsg) |
| 6512 | goto erret; |
| 6513 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6514 | if (line != NULL && *line == '|') |
| 6515 | // the line continues after a '|' |
| 6516 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6517 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6518 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6519 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6520 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6521 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6522 | goto erret; |
| 6523 | } |
| 6524 | else |
| 6525 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6526 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6527 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6528 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6529 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6530 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6531 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6532 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6533 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6534 | ea.cmdlinep = &line; |
| 6535 | ea.cmd = skipwhite(line); |
| 6536 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6537 | // Some things can be recognized by the first character. |
| 6538 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6539 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6540 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6541 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6542 | if (ea.cmd[1] != '{') |
| 6543 | { |
| 6544 | line = (char_u *)""; |
| 6545 | continue; |
| 6546 | } |
| 6547 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6548 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6549 | case '}': |
| 6550 | { |
| 6551 | // "}" ends a block scope |
| 6552 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6553 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6554 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6555 | if (stype == BLOCK_SCOPE) |
| 6556 | { |
| 6557 | compile_endblock(&cctx); |
| 6558 | line = ea.cmd; |
| 6559 | } |
| 6560 | else |
| 6561 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6562 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6563 | goto erret; |
| 6564 | } |
| 6565 | if (line != NULL) |
| 6566 | line = skipwhite(ea.cmd + 1); |
| 6567 | continue; |
| 6568 | } |
| 6569 | |
| 6570 | case '{': |
| 6571 | // "{" starts a block scope |
| 6572 | // "{'a': 1}->func() is something else |
| 6573 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6574 | { |
| 6575 | line = compile_block(ea.cmd, &cctx); |
| 6576 | continue; |
| 6577 | } |
| 6578 | break; |
| 6579 | |
| 6580 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6581 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6582 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | /* |
| 6586 | * COMMAND MODIFIERS |
| 6587 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6588 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6589 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6590 | { |
| 6591 | if (errormsg != NULL) |
| 6592 | goto erret; |
| 6593 | // empty line or comment |
| 6594 | line = (char_u *)""; |
| 6595 | continue; |
| 6596 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6597 | // TODO: use modifiers in the command |
| 6598 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6599 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6600 | |
| 6601 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6602 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6603 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6604 | { |
| 6605 | if (*ea.cmd == '(') |
| 6606 | // not for "call()" |
| 6607 | ea.cmd = p; |
| 6608 | else |
| 6609 | ea.cmd = skipwhite(ea.cmd); |
| 6610 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6611 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6612 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6613 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6614 | char_u *pskip; |
| 6615 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6616 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6617 | // find what follows. |
| 6618 | // Skip over "var.member", "var[idx]" and the like. |
| 6619 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6620 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6621 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6622 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6623 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6624 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6625 | char_u *var_end; |
| 6626 | int oplen; |
| 6627 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6628 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6629 | if (ea.cmd[0] == '@') |
| 6630 | var_end = ea.cmd + 2; |
| 6631 | else |
| 6632 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6633 | FNE_CHECK_START | FNE_INCL_BR); |
| 6634 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6635 | if (oplen > 0) |
| 6636 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6637 | size_t len = p - ea.cmd; |
| 6638 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6639 | // Recognize an assignment if we recognize the variable |
| 6640 | // name: |
| 6641 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6642 | // "local = expr" where "local" is a local var. |
| 6643 | // "script = expr" where "script" is a script-local var. |
| 6644 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6645 | // "&opt = expr" |
| 6646 | // "$ENV = expr" |
| 6647 | // "@r = expr" |
| 6648 | if (*ea.cmd == '&' |
| 6649 | || *ea.cmd == '$' |
| 6650 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6651 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6652 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6653 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6654 | NULL, &cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 6655 | || lookup_script(ea.cmd, len, FALSE) == OK |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6656 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6657 | { |
| 6658 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6659 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6660 | goto erret; |
| 6661 | continue; |
| 6662 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6663 | } |
| 6664 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6665 | |
| 6666 | if (*ea.cmd == '[') |
| 6667 | { |
| 6668 | // [var, var] = expr |
| 6669 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6670 | if (line == NULL) |
| 6671 | goto erret; |
| 6672 | if (line != ea.cmd) |
| 6673 | continue; |
| 6674 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | /* |
| 6678 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6679 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6680 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6681 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 6682 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6683 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6684 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6685 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6686 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6687 | if (!starts_with_colon) |
| 6688 | { |
| 6689 | emsg(_(e_colon_required_before_a_range)); |
| 6690 | goto erret; |
| 6691 | } |
| 6692 | if (ends_excmd2(line, ea.cmd)) |
| 6693 | { |
| 6694 | // A range without a command: jump to the line. |
| 6695 | // TODO: compile to a more efficient command, possibly |
| 6696 | // calling parse_cmd_address(). |
| 6697 | ea.cmdidx = CMD_SIZE; |
| 6698 | line = compile_exec(line, &ea, &cctx); |
| 6699 | goto nextline; |
| 6700 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6701 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6702 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6703 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6704 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6705 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6706 | |
| 6707 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6708 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6709 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6710 | { |
| 6711 | line += STRLEN(line); |
| 6712 | continue; |
| 6713 | } |
| 6714 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6715 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6716 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6717 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6718 | // CMD_let cannot happen, compile_assignment() above is used |
| 6719 | iemsg("Command from find_ex_command() not handled"); |
| 6720 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6721 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6722 | } |
| 6723 | |
| 6724 | p = skipwhite(p); |
| 6725 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6726 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 6727 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6728 | && ea.cmdidx != CMD_elseif |
| 6729 | && ea.cmdidx != CMD_else |
| 6730 | && ea.cmdidx != CMD_endif) |
| 6731 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6732 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6733 | continue; |
| 6734 | } |
| 6735 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6736 | if (ea.cmdidx != CMD_elseif |
| 6737 | && ea.cmdidx != CMD_else |
| 6738 | && ea.cmdidx != CMD_endif |
| 6739 | && ea.cmdidx != CMD_endfor |
| 6740 | && ea.cmdidx != CMD_endwhile |
| 6741 | && ea.cmdidx != CMD_catch |
| 6742 | && ea.cmdidx != CMD_finally |
| 6743 | && ea.cmdidx != CMD_endtry) |
| 6744 | { |
| 6745 | if (cctx.ctx_had_return) |
| 6746 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6747 | emsg(_(e_unreachable_code_after_return)); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6748 | goto erret; |
| 6749 | } |
| 6750 | } |
| 6751 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6752 | switch (ea.cmdidx) |
| 6753 | { |
| 6754 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6755 | ea.arg = p; |
| 6756 | line = compile_nested_function(&ea, &cctx); |
| 6757 | break; |
| 6758 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6759 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6760 | // TODO: should we allow this, e.g. to declare a global |
| 6761 | // function? |
| 6762 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6763 | goto erret; |
| 6764 | |
| 6765 | case CMD_return: |
| 6766 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6767 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6768 | break; |
| 6769 | |
| 6770 | case CMD_let: |
| 6771 | case CMD_const: |
| 6772 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6773 | if (line == p) |
| 6774 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6775 | break; |
| 6776 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6777 | case CMD_unlet: |
| 6778 | case CMD_unlockvar: |
| 6779 | case CMD_lockvar: |
| 6780 | line = compile_unletlock(p, &ea, &cctx); |
| 6781 | break; |
| 6782 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6783 | case CMD_import: |
| 6784 | line = compile_import(p, &cctx); |
| 6785 | break; |
| 6786 | |
| 6787 | case CMD_if: |
| 6788 | line = compile_if(p, &cctx); |
| 6789 | break; |
| 6790 | case CMD_elseif: |
| 6791 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6792 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6793 | break; |
| 6794 | case CMD_else: |
| 6795 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6796 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6797 | break; |
| 6798 | case CMD_endif: |
| 6799 | line = compile_endif(p, &cctx); |
| 6800 | break; |
| 6801 | |
| 6802 | case CMD_while: |
| 6803 | line = compile_while(p, &cctx); |
| 6804 | break; |
| 6805 | case CMD_endwhile: |
| 6806 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6807 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6808 | break; |
| 6809 | |
| 6810 | case CMD_for: |
| 6811 | line = compile_for(p, &cctx); |
| 6812 | break; |
| 6813 | case CMD_endfor: |
| 6814 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6815 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6816 | break; |
| 6817 | case CMD_continue: |
| 6818 | line = compile_continue(p, &cctx); |
| 6819 | break; |
| 6820 | case CMD_break: |
| 6821 | line = compile_break(p, &cctx); |
| 6822 | break; |
| 6823 | |
| 6824 | case CMD_try: |
| 6825 | line = compile_try(p, &cctx); |
| 6826 | break; |
| 6827 | case CMD_catch: |
| 6828 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6829 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6830 | break; |
| 6831 | case CMD_finally: |
| 6832 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6833 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6834 | break; |
| 6835 | case CMD_endtry: |
| 6836 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6837 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6838 | break; |
| 6839 | case CMD_throw: |
| 6840 | line = compile_throw(p, &cctx); |
| 6841 | break; |
| 6842 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6843 | case CMD_eval: |
| 6844 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6845 | goto erret; |
| 6846 | |
| 6847 | // drop the return value |
| 6848 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6849 | |
| 6850 | line = skipwhite(p); |
| 6851 | break; |
| 6852 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6853 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6854 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6855 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6856 | case CMD_echomsg: |
| 6857 | case CMD_echoerr: |
| 6858 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6859 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6860 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6861 | // TODO: other commands with an expression argument |
| 6862 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6863 | case CMD_append: |
| 6864 | case CMD_change: |
| 6865 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 6866 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6867 | case CMD_xit: |
| 6868 | not_in_vim9(&ea); |
| 6869 | goto erret; |
| 6870 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6871 | case CMD_SIZE: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6872 | semsg(_(e_invalid_command_str), ea.cmd); |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6873 | goto erret; |
| 6874 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6875 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6876 | // Not recognized, execute with do_cmdline_cmd(). |
| 6877 | ea.arg = p; |
| 6878 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6879 | break; |
| 6880 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6881 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6882 | if (line == NULL) |
| 6883 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6884 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6885 | |
| 6886 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6887 | { |
| 6888 | iemsg("Type stack underflow"); |
| 6889 | goto erret; |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | if (cctx.ctx_scope != NULL) |
| 6894 | { |
| 6895 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6896 | emsg(_(e_endif)); |
| 6897 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6898 | emsg(_(e_endwhile)); |
| 6899 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6900 | emsg(_(e_endfor)); |
| 6901 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6902 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6903 | goto erret; |
| 6904 | } |
| 6905 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6906 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6907 | { |
| 6908 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6909 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6910 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6911 | goto erret; |
| 6912 | } |
| 6913 | |
| 6914 | // Return zero if there is no return at the end. |
| 6915 | generate_PUSHNR(&cctx, 0); |
| 6916 | generate_instr(&cctx, ISN_RETURN); |
| 6917 | } |
| 6918 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6919 | { |
| 6920 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6921 | + ufunc->uf_dfunc_idx; |
| 6922 | dfunc->df_deleted = FALSE; |
| 6923 | dfunc->df_instr = instr->ga_data; |
| 6924 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6925 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6926 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6927 | if (cctx.ctx_outer_used) |
| 6928 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6929 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6930 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6931 | |
| 6932 | ret = OK; |
| 6933 | |
| 6934 | erret: |
| 6935 | if (ret == FAIL) |
| 6936 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6937 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6938 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6939 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6940 | |
| 6941 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6942 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6943 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6944 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6945 | // If using the last entry in the table and it was added above, we |
| 6946 | // might as well remove it. |
| 6947 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 6948 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6949 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6950 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6951 | ufunc->uf_dfunc_idx = 0; |
| 6952 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6953 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6954 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6955 | while (cctx.ctx_scope != NULL) |
| 6956 | drop_scope(&cctx); |
| 6957 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6958 | // Don't execute this function body. |
| 6959 | ga_clear_strings(&ufunc->uf_lines); |
| 6960 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6961 | if (errormsg != NULL) |
| 6962 | emsg(errormsg); |
| 6963 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6964 | emsg(_(e_compile_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6968 | if (do_estack_push) |
| 6969 | estack_pop(); |
| 6970 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6971 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6972 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6973 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6974 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6975 | } |
| 6976 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 6977 | void |
| 6978 | set_function_type(ufunc_T *ufunc) |
| 6979 | { |
| 6980 | int varargs = ufunc->uf_va_name != NULL; |
| 6981 | int argcount = ufunc->uf_args.ga_len; |
| 6982 | |
| 6983 | // Create a type for the function, with the return type and any |
| 6984 | // argument types. |
| 6985 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6986 | // The type is included in "tt_args". |
| 6987 | if (argcount > 0 || varargs) |
| 6988 | { |
| 6989 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6990 | argcount, &ufunc->uf_type_list); |
| 6991 | // Add argument types to the function type. |
| 6992 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 6993 | argcount + varargs, |
| 6994 | &ufunc->uf_type_list) == FAIL) |
| 6995 | return; |
| 6996 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6997 | ufunc->uf_func_type->tt_min_argcount = |
| 6998 | argcount - ufunc->uf_def_args.ga_len; |
| 6999 | if (ufunc->uf_arg_types == NULL) |
| 7000 | { |
| 7001 | int i; |
| 7002 | |
| 7003 | // lambda does not have argument types. |
| 7004 | for (i = 0; i < argcount; ++i) |
| 7005 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7006 | } |
| 7007 | else |
| 7008 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7009 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7010 | if (varargs) |
| 7011 | { |
| 7012 | ufunc->uf_func_type->tt_args[argcount] = |
| 7013 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7014 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7015 | } |
| 7016 | } |
| 7017 | else |
| 7018 | // No arguments, can use a predefined type. |
| 7019 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7020 | argcount, &ufunc->uf_type_list); |
| 7021 | } |
| 7022 | |
| 7023 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7024 | /* |
| 7025 | * Delete an instruction, free what it contains. |
| 7026 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7027 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7028 | delete_instr(isn_T *isn) |
| 7029 | { |
| 7030 | switch (isn->isn_type) |
| 7031 | { |
| 7032 | case ISN_EXEC: |
| 7033 | case ISN_LOADENV: |
| 7034 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7035 | case ISN_LOADB: |
| 7036 | case ISN_LOADW: |
| 7037 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7038 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7039 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7040 | case ISN_PUSHEXC: |
| 7041 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7042 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7043 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7044 | case ISN_STOREB: |
| 7045 | case ISN_STOREW: |
| 7046 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7047 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7048 | vim_free(isn->isn_arg.string); |
| 7049 | break; |
| 7050 | |
| 7051 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7052 | case ISN_STORES: |
| 7053 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7054 | break; |
| 7055 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7056 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7057 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7058 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7059 | break; |
| 7060 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7061 | case ISN_STOREOPT: |
| 7062 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7063 | break; |
| 7064 | |
| 7065 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7066 | blob_unref(isn->isn_arg.blob); |
| 7067 | break; |
| 7068 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7069 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7070 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7071 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7072 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7073 | break; |
| 7074 | |
| 7075 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7076 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7077 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7078 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7079 | break; |
| 7080 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7081 | case ISN_UCALL: |
| 7082 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7083 | break; |
| 7084 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7085 | case ISN_FUNCREF: |
| 7086 | { |
| 7087 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7088 | + isn->isn_arg.funcref.fr_func; |
| 7089 | func_ptr_unref(dfunc->df_ufunc); |
| 7090 | } |
| 7091 | break; |
| 7092 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7093 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7094 | { |
| 7095 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7096 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7097 | |
| 7098 | if (ufunc != NULL) |
| 7099 | { |
| 7100 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7101 | clear_def_function(ufunc); |
| 7102 | ufunc->uf_dfunc_idx = 0; |
| 7103 | func_ptr_unref(ufunc); |
| 7104 | } |
| 7105 | |
| 7106 | vim_free(lambda); |
| 7107 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7108 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7109 | break; |
| 7110 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7111 | case ISN_2BOOL: |
| 7112 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 7113 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7114 | case ISN_ADDBLOB: |
| 7115 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7116 | case ISN_ANYINDEX: |
| 7117 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7118 | case ISN_BCALL: |
| 7119 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7120 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7121 | case ISN_CHECKNR: |
| 7122 | case ISN_CHECKTYPE: |
| 7123 | case ISN_COMPAREANY: |
| 7124 | case ISN_COMPAREBLOB: |
| 7125 | case ISN_COMPAREBOOL: |
| 7126 | case ISN_COMPAREDICT: |
| 7127 | case ISN_COMPAREFLOAT: |
| 7128 | case ISN_COMPAREFUNC: |
| 7129 | case ISN_COMPARELIST: |
| 7130 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7131 | case ISN_COMPARESPECIAL: |
| 7132 | case ISN_COMPARESTRING: |
| 7133 | case ISN_CONCAT: |
| 7134 | case ISN_DCALL: |
| 7135 | case ISN_DROP: |
| 7136 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7137 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7138 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7139 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7140 | case ISN_EXECCONCAT: |
| 7141 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7142 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7143 | case ISN_GETITEM: |
| 7144 | case ISN_JUMP: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7145 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 7146 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7147 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7148 | case ISN_LOADBDICT: |
| 7149 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7150 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7151 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7152 | case ISN_LOADSCRIPT: |
| 7153 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7154 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7155 | case ISN_LOADWDICT: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7156 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7157 | case ISN_NEGATENR: |
| 7158 | case ISN_NEWDICT: |
| 7159 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7160 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7161 | case ISN_OPFLOAT: |
| 7162 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7163 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7164 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7165 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7166 | case ISN_PUSHF: |
| 7167 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7168 | case ISN_PUSHSPEC: |
| 7169 | case ISN_RETURN: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7170 | case ISN_SHUFFLE: |
| 7171 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7172 | case ISN_STORE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7173 | case ISN_STOREDICT: |
| 7174 | case ISN_STORELIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7175 | case ISN_STORENR: |
| 7176 | case ISN_STOREOUTER: |
| 7177 | case ISN_STOREREG: |
| 7178 | case ISN_STORESCRIPT: |
| 7179 | case ISN_STOREV: |
| 7180 | case ISN_STRINDEX: |
| 7181 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7182 | case ISN_THROW: |
| 7183 | case ISN_TRY: |
| 7184 | // nothing allocated |
| 7185 | break; |
| 7186 | } |
| 7187 | } |
| 7188 | |
| 7189 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7190 | * Free all instructions for "dfunc". |
| 7191 | */ |
| 7192 | static void |
| 7193 | delete_def_function_contents(dfunc_T *dfunc) |
| 7194 | { |
| 7195 | int idx; |
| 7196 | |
| 7197 | ga_clear(&dfunc->df_def_args_isn); |
| 7198 | |
| 7199 | if (dfunc->df_instr != NULL) |
| 7200 | { |
| 7201 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7202 | delete_instr(dfunc->df_instr + idx); |
| 7203 | VIM_CLEAR(dfunc->df_instr); |
| 7204 | } |
| 7205 | |
| 7206 | dfunc->df_deleted = TRUE; |
| 7207 | } |
| 7208 | |
| 7209 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7210 | * When a user function is deleted, clear the contents of any associated def |
| 7211 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7212 | */ |
| 7213 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7214 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7215 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7216 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7217 | { |
| 7218 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7219 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7220 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7221 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7222 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7227 | /* |
| 7228 | * Free all functions defined with ":def". |
| 7229 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7230 | void |
| 7231 | free_def_functions(void) |
| 7232 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7233 | int idx; |
| 7234 | |
| 7235 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7236 | { |
| 7237 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7238 | |
| 7239 | delete_def_function_contents(dfunc); |
| 7240 | } |
| 7241 | |
| 7242 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7243 | } |
| 7244 | #endif |
| 7245 | |
| 7246 | |
| 7247 | #endif // FEAT_EVAL |