blob: f1da32318887d128f17c2fde9fd746a5a62febc8 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* 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
14typedef enum {
15 ISN_EXEC, // execute Ex command line isn_arg.string
Bram Moolenaarad39c092020-02-26 18:23:43 +010016 ISN_ECHO, // echo isn_arg.echo.echo_count items on top of stack
17 ISN_EXECUTE, // execute Ex commands isn_arg.number items on top of stack
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +020018 ISN_ECHOMSG, // echo Ex commands isn_arg.number items on top of stack
19 ISN_ECHOERR, // echo Ex commands isn_arg.number items on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010020
21 // get and set variables
22 ISN_LOAD, // push local variable isn_arg.number
23 ISN_LOADV, // push v: variable isn_arg.number
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024 ISN_LOADG, // push g: variable isn_arg.string
Bram Moolenaard3aac292020-04-19 14:32:17 +020025 ISN_LOADB, // push b: variable isn_arg.string
26 ISN_LOADW, // push w: variable isn_arg.string
27 ISN_LOADT, // push t: variable isn_arg.string
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010028 ISN_LOADS, // push s: variable isn_arg.loadstore
29 ISN_LOADSCRIPT, // push script-local variable isn_arg.script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010030 ISN_LOADOPT, // push option isn_arg.string
31 ISN_LOADENV, // push environment variable isn_arg.string
32 ISN_LOADREG, // push register isn_arg.number
33
34 ISN_STORE, // pop into local variable isn_arg.number
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010035 ISN_STOREV, // pop into v: variable isn_arg.number
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 ISN_STOREG, // pop into global variable isn_arg.string
Bram Moolenaard3aac292020-04-19 14:32:17 +020037 ISN_STOREB, // pop into buffer-local variable isn_arg.string
38 ISN_STOREW, // pop into window-local variable isn_arg.string
39 ISN_STORET, // pop into tab-local variable isn_arg.string
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +020040 ISN_STORES, // pop into script variable isn_arg.loadstore
41 ISN_STORESCRIPT, // pop into script variable isn_arg.script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042 ISN_STOREOPT, // pop into option isn_arg.string
Bram Moolenaarb283a8a2020-02-02 22:24:04 +010043 ISN_STOREENV, // pop into environment variable isn_arg.string
44 ISN_STOREREG, // pop into register isn_arg.number
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045 // ISN_STOREOTHER, // pop into other script variable isn_arg.other.
46
Bram Moolenaara471eea2020-03-04 22:20:26 +010047 ISN_STORENR, // store number into local variable isn_arg.storenr.stnr_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020049 ISN_UNLET, // unlet variable isn_arg.unlet.ul_name
Bram Moolenaar7bdaea62020-04-19 18:27:26 +020050 ISN_UNLETENV, // unlet environment variable isn_arg.unlet.ul_name
Bram Moolenaard72c1bf2020-04-19 16:28:59 +020051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010052 // constants
Bram Moolenaar42a480b2020-02-29 23:23:47 +010053 ISN_PUSHNR, // push number isn_arg.number
54 ISN_PUSHBOOL, // push bool value isn_arg.number
55 ISN_PUSHSPEC, // push special value isn_arg.number
56 ISN_PUSHF, // push float isn_arg.fnumber
57 ISN_PUSHS, // push string isn_arg.string
58 ISN_PUSHBLOB, // push blob isn_arg.blob
59 ISN_PUSHFUNC, // push func isn_arg.string
Bram Moolenaar42a480b2020-02-29 23:23:47 +010060 ISN_PUSHCHANNEL, // push channel isn_arg.channel
61 ISN_PUSHJOB, // push channel isn_arg.job
62 ISN_NEWLIST, // push list from stack items, size is isn_arg.number
63 ISN_NEWDICT, // push dict from stack items, size is isn_arg.number
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064
65 // function call
66 ISN_BCALL, // call builtin function isn_arg.bfunc
67 ISN_DCALL, // call def function isn_arg.dfunc
68 ISN_UCALL, // call user function or funcref/partial isn_arg.ufunc
69 ISN_PCALL, // call partial, use isn_arg.pfunc
Bram Moolenaarbd5da372020-03-31 23:13:10 +020070 ISN_PCALL_END, // cleanup after ISN_PCALL with cpf_top set
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071 ISN_RETURN, // return, result is on top of stack
72 ISN_FUNCREF, // push a function ref to dfunc isn_arg.number
73
74 // expression operations
75 ISN_JUMP, // jump if condition is matched isn_arg.jump
76
77 // loop
78 ISN_FOR, // get next item from a list, uses isn_arg.forloop
79
80 ISN_TRY, // add entry to ec_trystack, uses isn_arg.try
81 ISN_THROW, // pop value of stack, store in v:exception
82 ISN_PUSHEXC, // push v:exception
83 ISN_CATCH, // drop v:exception
84 ISN_ENDTRY, // take entry off from ec_trystack
85
86 // moreexpression operations
87 ISN_ADDLIST,
88 ISN_ADDBLOB,
89
90 // operation with two arguments; isn_arg.op.op_type is exptype_T
91 ISN_OPNR,
92 ISN_OPFLOAT,
93 ISN_OPANY,
94
95 // comparative operations; isn_arg.op.op_type is exptype_T, op_ic used
96 ISN_COMPAREBOOL,
97 ISN_COMPARESPECIAL,
98 ISN_COMPARENR,
99 ISN_COMPAREFLOAT,
100 ISN_COMPARESTRING,
101 ISN_COMPAREBLOB,
102 ISN_COMPARELIST,
103 ISN_COMPAREDICT,
104 ISN_COMPAREFUNC,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100105 ISN_COMPAREANY,
106
107 // expression operations
108 ISN_CONCAT,
109 ISN_INDEX, // [expr] list index
110 ISN_MEMBER, // dict.member using isn_arg.string
111 ISN_2BOOL, // convert value to bool, invert if isn_arg.number != 0
112 ISN_2STRING, // convert value to string at isn_arg.number on stack
113 ISN_NEGATENR, // apply "-" to number
114
115 ISN_CHECKNR, // check value can be used as a number
116 ISN_CHECKTYPE, // check value type is isn_arg.type.tc_type
117
118 ISN_DROP // pop stack and discard value
119} isntype_T;
120
121
122// arguments to ISN_BCALL
123typedef struct {
124 int cbf_idx; // index in "global_functions"
125 int cbf_argcount; // number of arguments on top of stack
126} cbfunc_T;
127
128// arguments to ISN_DCALL
129typedef struct {
130 int cdf_idx; // index in "def_functions" for ISN_DCALL
131 int cdf_argcount; // number of arguments on top of stack
132} cdfunc_T;
133
134// arguments to ISN_PCALL
135typedef struct {
136 int cpf_top; // when TRUE partial is above the arguments
137 int cpf_argcount; // number of arguments on top of stack
138} cpfunc_T;
139
140// arguments to ISN_UCALL and ISN_XCALL
141typedef struct {
142 char_u *cuf_name;
143 int cuf_argcount; // number of arguments on top of stack
144} cufunc_T;
145
146typedef enum {
147 JUMP_ALWAYS,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 JUMP_IF_FALSE, // pop and jump if false
149 JUMP_AND_KEEP_IF_TRUE, // jump if top of stack is true, drop if not
150 JUMP_AND_KEEP_IF_FALSE, // jump if top of stack is false, drop if not
151} jumpwhen_T;
152
153// arguments to ISN_JUMP
154typedef struct {
155 jumpwhen_T jump_when;
156 int jump_where; // position to jump to
157} jump_T;
158
159// arguments to ISN_FOR
160typedef struct {
161 int for_idx; // loop variable index
162 int for_end; // position to jump to after done
163} forloop_T;
164
165// arguments to ISN_TRY
166typedef struct {
167 int try_catch; // position to jump to on throw
168 int try_finally; // position to jump to for return
169} try_T;
170
171// arguments to ISN_ECHO
172typedef struct {
173 int echo_with_white; // :echo instead of :echon
174 int echo_count; // number of expressions
175} echo_T;
176
177// arguments to ISN_OPNR, ISN_OPFLOAT, etc.
178typedef struct {
179 exptype_T op_type;
180 int op_ic; // TRUE with '#', FALSE with '?', else MAYBE
181} opexpr_T;
182
183// arguments to ISN_CHECKTYPE
184typedef struct {
185 vartype_T ct_type;
186 int ct_off; // offset in stack, -1 is bottom
187} checktype_T;
188
189// arguments to ISN_STORENR
190typedef struct {
Bram Moolenaara471eea2020-03-04 22:20:26 +0100191 int stnr_idx;
192 varnumber_T stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193} storenr_T;
194
195// arguments to ISN_STOREOPT
196typedef struct {
197 char_u *so_name;
198 int so_flags;
199} storeopt_T;
200
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100201// arguments to ISN_LOADS and ISN_STORES
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202typedef struct {
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200203 char_u *ls_name; // variable name (with s: for ISN_STORES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 int ls_sid; // script ID
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100205} loadstore_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100207// arguments to ISN_LOADSCRIPT and ISN_STORESCRIPT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208typedef struct {
209 int script_sid; // script ID
210 int script_idx; // index in sn_var_vals
211} script_T;
212
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200213// arguments to ISN_UNLET
214typedef struct {
215 char_u *ul_name; // variable name with g:, w:, etc.
216 int ul_forceit; // forceit flag
217} unlet_T;
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
220 * Instruction
221 */
Bram Moolenaar20431c92020-03-20 18:39:46 +0100222struct isn_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223 isntype_T isn_type;
224 int isn_lnum;
225 union {
226 char_u *string;
227 varnumber_T number;
228 blob_T *blob;
229#ifdef FEAT_FLOAT
230 float_T fnumber;
231#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100232 channel_T *channel;
233 job_T *job;
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100234 partial_T *partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 jump_T jump;
236 forloop_T forloop;
237 try_T try;
238 cbfunc_T bfunc;
239 cdfunc_T dfunc;
240 cpfunc_T pfunc;
241 cufunc_T ufunc;
242 echo_T echo;
243 opexpr_T op;
244 checktype_T type;
245 storenr_T storenr;
246 storeopt_T storeopt;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100247 loadstore_T loadstore;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 script_T script;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200249 unlet_T unlet;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 } isn_arg;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100251};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
253/*
254 * Info about a function defined with :def. Used in "def_functions".
255 */
256struct dfunc_S {
257 ufunc_T *df_ufunc; // struct containing most stuff
258 int df_idx; // index in def_functions
259 int df_deleted; // if TRUE function was deleted
260
261 garray_T df_def_args_isn; // default argument instructions
262 isn_T *df_instr; // function body to be executed
263 int df_instr_count;
264
265 int df_varcount; // number of local variables
266};
267
268// Number of entries used by stack frame for a function call.
269#define STACK_FRAME_SIZE 3
270
271
272#ifdef DEFINE_VIM9_GLOBALS
273// Functions defined with :def are stored in this growarray.
274// They are never removed, so that they can be found by index.
275// Deleted functions have the df_deleted flag set.
Bram Moolenaar05afcee2020-03-31 23:32:31 +0200276garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277#else
278extern garray_T def_functions;
279#endif
280