xref: /vim-8.2.3635/src/vim9.h (revision abcbb0e9)
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  * vim9.h: types and globals used for Vim9 script.
12  */
13 
14 typedef enum {
15     ISN_EXEC,	    // execute Ex command line isn_arg.string
16     ISN_EXECCONCAT, // execute Ex command from isn_arg.number items on stack
17     ISN_ECHO,	    // echo isn_arg.echo.echo_count items on top of stack
18     ISN_EXECUTE,    // execute Ex commands isn_arg.number items on top of stack
19     ISN_ECHOMSG,    // echo Ex commands isn_arg.number items on top of stack
20     ISN_ECHOERR,    // echo Ex commands isn_arg.number items on top of stack
21     ISN_RANGE,	    // compute range from isn_arg.string, push to stack
22 
23     // get and set variables
24     ISN_LOAD,	    // push local variable isn_arg.number
25     ISN_LOADV,	    // push v: variable isn_arg.number
26     ISN_LOADG,	    // push g: variable isn_arg.string
27     ISN_LOADAUTO,   // push g: autoload variable isn_arg.string
28     ISN_LOADB,	    // push b: variable isn_arg.string
29     ISN_LOADW,	    // push w: variable isn_arg.string
30     ISN_LOADT,	    // push t: variable isn_arg.string
31     ISN_LOADGDICT,  // push g: dict
32     ISN_LOADBDICT,  // push b: dict
33     ISN_LOADWDICT,  // push w: dict
34     ISN_LOADTDICT,  // push t: dict
35     ISN_LOADS,	    // push s: variable isn_arg.loadstore
36     ISN_LOADOUTER,  // push variable from outer scope isn_arg.number
37     ISN_LOADSCRIPT, // push script-local variable isn_arg.script.
38     ISN_LOADOPT,    // push option isn_arg.string
39     ISN_LOADENV,    // push environment variable isn_arg.string
40     ISN_LOADREG,    // push register isn_arg.number
41 
42     ISN_STORE,	    // pop into local variable isn_arg.number
43     ISN_STOREV,	    // pop into v: variable isn_arg.number
44     ISN_STOREG,	    // pop into global variable isn_arg.string
45     ISN_STOREAUTO,  // pop into global autoload variable isn_arg.string
46     ISN_STOREB,	    // pop into buffer-local variable isn_arg.string
47     ISN_STOREW,	    // pop into window-local variable isn_arg.string
48     ISN_STORET,	    // pop into tab-local variable isn_arg.string
49     ISN_STORES,	    // pop into script variable isn_arg.loadstore
50     ISN_STOREOUTER,  // pop variable into outer scope isn_arg.number
51     ISN_STORESCRIPT, // pop into script variable isn_arg.script
52     ISN_STOREOPT,    // pop into option isn_arg.string
53     ISN_STOREENV,    // pop into environment variable isn_arg.string
54     ISN_STOREREG,    // pop into register isn_arg.number
55     // ISN_STOREOTHER, // pop into other script variable isn_arg.other.
56 
57     ISN_STORENR,    // store number into local variable isn_arg.storenr.stnr_idx
58     ISN_STOREINDEX,	// store into list or dictionary, type isn_arg.vartype,
59 			// value/index/variable on stack
60 
61     ISN_UNLET,		// unlet variable isn_arg.unlet.ul_name
62     ISN_UNLETENV,	// unlet environment variable isn_arg.unlet.ul_name
63 
64     ISN_LOCKCONST,	// lock constant value
65 
66     // constants
67     ISN_PUSHNR,		// push number isn_arg.number
68     ISN_PUSHBOOL,	// push bool value isn_arg.number
69     ISN_PUSHSPEC,	// push special value isn_arg.number
70     ISN_PUSHF,		// push float isn_arg.fnumber
71     ISN_PUSHS,		// push string isn_arg.string
72     ISN_PUSHBLOB,	// push blob isn_arg.blob
73     ISN_PUSHFUNC,	// push func isn_arg.string
74     ISN_PUSHCHANNEL,	// push channel isn_arg.channel
75     ISN_PUSHJOB,	// push channel isn_arg.job
76     ISN_NEWLIST,	// push list from stack items, size is isn_arg.number
77     ISN_NEWDICT,	// push dict from stack items, size is isn_arg.number
78 
79     // function call
80     ISN_BCALL,	    // call builtin function isn_arg.bfunc
81     ISN_DCALL,	    // call def function isn_arg.dfunc
82     ISN_UCALL,	    // call user function or funcref/partial isn_arg.ufunc
83     ISN_PCALL,	    // call partial, use isn_arg.pfunc
84     ISN_PCALL_END,  // cleanup after ISN_PCALL with cpf_top set
85     ISN_RETURN,	    // return, result is on top of stack
86     ISN_FUNCREF,    // push a function ref to dfunc isn_arg.funcref
87     ISN_NEWFUNC,    // create a global function from a lambda function
88     ISN_DEF,	    // list functions
89 
90     // expression operations
91     ISN_JUMP,	    // jump if condition is matched isn_arg.jump
92 
93     // loop
94     ISN_FOR,	    // get next item from a list, uses isn_arg.forloop
95 
96     ISN_TRY,	    // add entry to ec_trystack, uses isn_arg.try
97     ISN_THROW,	    // pop value of stack, store in v:exception
98     ISN_PUSHEXC,    // push v:exception
99     ISN_CATCH,	    // drop v:exception
100     ISN_ENDTRY,	    // take entry off from ec_trystack
101 
102     // more expression operations
103     ISN_ADDLIST,    // add two lists
104     ISN_ADDBLOB,    // add two blobs
105 
106     // operation with two arguments; isn_arg.op.op_type is exptype_T
107     ISN_OPNR,
108     ISN_OPFLOAT,
109     ISN_OPANY,
110 
111     // comparative operations; isn_arg.op.op_type is exptype_T, op_ic used
112     ISN_COMPAREBOOL,
113     ISN_COMPARESPECIAL,
114     ISN_COMPARENR,
115     ISN_COMPAREFLOAT,
116     ISN_COMPARESTRING,
117     ISN_COMPAREBLOB,
118     ISN_COMPARELIST,
119     ISN_COMPAREDICT,
120     ISN_COMPAREFUNC,
121     ISN_COMPAREANY,
122 
123     // expression operations
124     ISN_CONCAT,
125     ISN_STRINDEX,   // [expr] string index
126     ISN_STRSLICE,   // [expr:expr] string slice
127     ISN_LISTAPPEND, // append to a list, like add()
128     ISN_LISTINDEX,  // [expr] list index
129     ISN_LISTSLICE,  // [expr:expr] list slice
130     ISN_ANYINDEX,   // [expr] runtime index
131     ISN_ANYSLICE,   // [expr:expr] runtime slice
132     ISN_SLICE,	    // drop isn_arg.number items from start of list
133     ISN_BLOBAPPEND, // append to a blob, like add()
134     ISN_GETITEM,    // push list item, isn_arg.number is the index
135     ISN_MEMBER,	    // dict[member]
136     ISN_STRINGMEMBER, // dict.member using isn_arg.string
137     ISN_2BOOL,	    // falsy/truthy to bool, invert if isn_arg.number != 0
138     ISN_COND2BOOL,  // convert value to bool
139     ISN_2STRING,    // convert value to string at isn_arg.number on stack
140     ISN_2STRING_ANY, // like ISN_2STRING but check type
141     ISN_NEGATENR,   // apply "-" to number
142 
143     ISN_CHECKNR,    // check value can be used as a number
144     ISN_CHECKTYPE,  // check value type is isn_arg.type.tc_type
145     ISN_CHECKLEN,   // check list length is isn_arg.checklen.cl_min_len
146 
147     ISN_PUT,	    // ":put", uses isn_arg.put
148 
149     ISN_CMDMOD,	    // set cmdmod
150     ISN_CMDMOD_REV, // undo ISN_CMDMOD
151 
152     ISN_UNPACK,	    // unpack list into items, uses isn_arg.unpack
153     ISN_SHUFFLE,    // move item on stack up or down
154     ISN_DROP	    // pop stack and discard value
155 } isntype_T;
156 
157 
158 // arguments to ISN_BCALL
159 typedef struct {
160     int	    cbf_idx;	    // index in "global_functions"
161     int	    cbf_argcount;   // number of arguments on top of stack
162 } cbfunc_T;
163 
164 // arguments to ISN_DCALL
165 typedef struct {
166     int	    cdf_idx;	    // index in "def_functions" for ISN_DCALL
167     int	    cdf_argcount;   // number of arguments on top of stack
168 } cdfunc_T;
169 
170 // arguments to ISN_PCALL
171 typedef struct {
172     int	    cpf_top;	    // when TRUE partial is above the arguments
173     int	    cpf_argcount;   // number of arguments on top of stack
174 } cpfunc_T;
175 
176 // arguments to ISN_UCALL and ISN_XCALL
177 typedef struct {
178     char_u  *cuf_name;
179     int	    cuf_argcount;   // number of arguments on top of stack
180 } cufunc_T;
181 
182 typedef enum {
183     JUMP_ALWAYS,
184     JUMP_IF_FALSE,		// pop and jump if false
185     JUMP_AND_KEEP_IF_TRUE,	// jump if top of stack is truthy, drop if not
186     JUMP_AND_KEEP_IF_FALSE,	// jump if top of stack is falsy, drop if not
187     JUMP_IF_COND_TRUE,		// jump if top of stack is true, drop if not
188     JUMP_IF_COND_FALSE,		// jump if top of stack is false, drop if not
189 } jumpwhen_T;
190 
191 // arguments to ISN_JUMP
192 typedef struct {
193     jumpwhen_T	jump_when;
194     int		jump_where;	    // position to jump to
195 } jump_T;
196 
197 // arguments to ISN_FOR
198 typedef struct {
199     int	    for_idx;	    // loop variable index
200     int	    for_end;	    // position to jump to after done
201 } forloop_T;
202 
203 // arguments to ISN_TRY
204 typedef struct {
205     int	    try_catch;	    // position to jump to on throw
206     int	    try_finally;    // position to jump to for return
207 } try_T;
208 
209 // arguments to ISN_ECHO
210 typedef struct {
211     int	    echo_with_white;    // :echo instead of :echon
212     int	    echo_count;		// number of expressions
213 } echo_T;
214 
215 // arguments to ISN_OPNR, ISN_OPFLOAT, etc.
216 typedef struct {
217     exptype_T	op_type;
218     int		op_ic;	    // TRUE with '#', FALSE with '?', else MAYBE
219 } opexpr_T;
220 
221 // arguments to ISN_CHECKTYPE
222 typedef struct {
223     type_T	*ct_type;
224     int		ct_off;	    // offset in stack, -1 is bottom
225 } checktype_T;
226 
227 // arguments to ISN_STORENR
228 typedef struct {
229     int		stnr_idx;
230     varnumber_T	stnr_val;
231 } storenr_T;
232 
233 // arguments to ISN_STOREOPT
234 typedef struct {
235     char_u	*so_name;
236     int		so_flags;
237 } storeopt_T;
238 
239 // arguments to ISN_LOADS and ISN_STORES
240 typedef struct {
241     char_u	*ls_name;	// variable name (with s: for ISN_STORES)
242     int		ls_sid;		// script ID
243 } loadstore_T;
244 
245 // arguments to ISN_LOADSCRIPT and ISN_STORESCRIPT
246 typedef struct {
247     int		script_sid;	// script ID
248     int		script_idx;	// index in sn_var_vals
249 } script_T;
250 
251 // arguments to ISN_UNLET
252 typedef struct {
253     char_u	*ul_name;	// variable name with g:, w:, etc.
254     int		ul_forceit;	// forceit flag
255 } unlet_T;
256 
257 // arguments to ISN_FUNCREF
258 typedef struct {
259     int		fr_func;	// function index
260 } funcref_T;
261 
262 // arguments to ISN_NEWFUNC
263 typedef struct {
264     char_u	*nf_lambda;	// name of the lambda already defined
265     char_u	*nf_global;	// name of the global function to be created
266 } newfunc_T;
267 
268 // arguments to ISN_CHECKLEN
269 typedef struct {
270     int		cl_min_len;	// minimum length
271     int		cl_more_OK;	// longer is allowed
272 } checklen_T;
273 
274 // arguments to ISN_SHUFFLE
275 typedef struct {
276     int		shfl_item;	// item to move (relative to top of stack)
277     int		shfl_up;	// places to move upwards
278 } shuffle_T;
279 
280 // arguments to ISN_PUT
281 typedef struct {
282     int		put_regname;	// register, can be NUL
283     linenr_T	put_lnum;	// line number to put below
284 } put_T;
285 
286 // arguments to ISN_CMDMOD
287 typedef struct {
288     cmdmod_T	*cf_cmdmod;	// allocated
289 } cmod_T;
290 
291 // arguments to ISN_UNPACK
292 typedef struct {
293     int		unp_count;	// number of items to produce
294     int		unp_semicolon;	// last item gets list of remainder
295 } unpack_T;
296 
297 /*
298  * Instruction
299  */
300 struct isn_S {
301     isntype_T	isn_type;
302     int		isn_lnum;
303     union {
304 	char_u		    *string;
305 	varnumber_T	    number;
306 	blob_T		    *blob;
307 	vartype_T	    vartype;
308 #ifdef FEAT_FLOAT
309 	float_T		    fnumber;
310 #endif
311 	channel_T	    *channel;
312 	job_T		    *job;
313 	partial_T	    *partial;
314 	jump_T		    jump;
315 	forloop_T	    forloop;
316 	try_T		    try;
317 	cbfunc_T	    bfunc;
318 	cdfunc_T	    dfunc;
319 	cpfunc_T	    pfunc;
320 	cufunc_T	    ufunc;
321 	echo_T		    echo;
322 	opexpr_T	    op;
323 	checktype_T	    type;
324 	storenr_T	    storenr;
325 	storeopt_T	    storeopt;
326 	loadstore_T	    loadstore;
327 	script_T	    script;
328 	unlet_T		    unlet;
329 	funcref_T	    funcref;
330 	newfunc_T	    newfunc;
331 	checklen_T	    checklen;
332 	shuffle_T	    shuffle;
333 	put_T		    put;
334 	cmod_T		    cmdmod;
335 	unpack_T	    unpack;
336     } isn_arg;
337 };
338 
339 /*
340  * Info about a function defined with :def.  Used in "def_functions".
341  */
342 struct dfunc_S {
343     ufunc_T	*df_ufunc;	    // struct containing most stuff
344     int		df_refcount;	    // how many ufunc_T point to this dfunc_T
345     int		df_idx;		    // index in def_functions
346     int		df_deleted;	    // if TRUE function was deleted
347     char_u	*df_name;	    // name used for error messages
348 
349     garray_T	df_def_args_isn;    // default argument instructions
350     isn_T	*df_instr;	    // function body to be executed
351     int		df_instr_count;
352 
353     int		df_varcount;	    // number of local variables
354     int		df_has_closure;	    // one if a closure was created
355 };
356 
357 // Number of entries used by stack frame for a function call.
358 // - ec_dfunc_idx:   function index
359 // - ec_iidx:        instruction index
360 // - ec_outer_stack: stack used for closures  TODO: can we avoid this?
361 // - ec_outer_frame: stack frame for closures
362 // - ec_frame_idx:   previous frame index
363 #define STACK_FRAME_SIZE 5
364 
365 
366 #ifdef DEFINE_VIM9_GLOBALS
367 // Functions defined with :def are stored in this growarray.
368 // They are never removed, so that they can be found by index.
369 // Deleted functions have the df_deleted flag set.
370 garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
371 #else
372 extern garray_T def_functions;
373 #endif
374 
375 // Used for "lnum" when a range is to be taken from the stack.
376 #define LNUM_VARIABLE_RANGE -999
377 
378 // Used for "lnum" when a range is to be taken from the stack and "!" is used.
379 #define LNUM_VARIABLE_RANGE_ABOVE -888
380