datex_core/global/
instruction_codes.rs

1use core::prelude::rust_2024::*;
2use num_enum::TryFromPrimitive;
3use strum::Display;
4use strum_macros::EnumIter;
5
6#[allow(non_camel_case_types)]
7#[derive(
8    EnumIter,
9    Debug,
10    Eq,
11    PartialEq,
12    TryFromPrimitive,
13    Copy,
14    Clone,
15    Display,
16    num_enum::IntoPrimitive,
17)]
18#[repr(u8)]
19pub enum InstructionCode {
20    // flow instructions 0x00 - 0x0f
21    EXIT = 0x00,
22    STATEMENTS,       // statements block
23    SHORT_STATEMENTS, // optimized statements block with up to 255 instructions
24    UNBOUNDED_STATEMENTS,
25    UNBOUNDED_STATEMENTS_END, // end of statements block (only needed for unbounded blocks)
26    CACHE_POINT,              // cache dxb from this point on
27    CACHE_RESET,              // reset dxb scope cache
28
29    // internal variables and other shorthands 0x30 - 0x4f
30    VAR_RESULT,
31    SET_VAR_RESULT,
32    SET_VAR_RESULT_REFERENCE,
33    VAR_RESULT_ACTION,
34
35    VAR_SUB_RESULT,
36    SET_VAR_SUB_RESULT,
37    SET_VAR_SUB_RESULT_REFERENCE,
38    VAR_SUB_RESULT_ACTION,
39
40    VAR_VOID,
41    SET_VAR_VOID,
42    SET_VAR_VOID_REFERENCE,
43    VAR_VOID_ACTION,
44
45    _VAR_ORIGIN,
46    _SET_VAR_ORIGIN,
47    _SET_VAR_ORIGIN_REFERENCE,
48    _VAR_ORIGIN_ACTION,
49
50    VAR_IT,
51    SET_VAR_IT,
52    SET_VAR_IT_REFERENCE,
53    VAR_IT_ACTION,
54
55    VAR_REMOTE,
56
57    VAR_REMOTE_ACTION,
58    VAR_ORIGIN,
59    VAR_ENDPOINT,
60    VAR_ENTRYPOINT,
61    VAR_STD,
62    // VAR_TIMESTAMP      ,
63    VAR_META,
64    VAR_PUBLIC,
65    VAR_THIS,
66    VAR_LOCATION,
67    VAR_ENV,
68
69    APPLY_ZERO,
70    APPLY_SINGLE,
71    APPLY,
72
73    GET_PROPERTY_DYNAMIC, // get property with arbitrary key value
74    GET_PROPERTY_INDEX,   // get property with integer index
75    GET_PROPERTY_TEXT,    // get property with text key
76
77    SET_PROPERTY_DYNAMIC, // set property with arbitrary key value
78    SET_PROPERTY_INDEX,   // set property with integer index
79    SET_PROPERTY_TEXT,    // set property with text key
80
81    // runtime commands 0x50 - 0x7f
82    RETURN,         // return
83    TEMPLATE,       // template
84    EXTENDS,        // extends
85    IMPLEMENTS,     // implements
86    MATCHES,        // matches
87    DEBUGGER,       // debugger
88    JMP,            // jmp labelname
89    JTR,            // jtr labelname
90    JFA,            // jfa labelname (TODO replace with 0xa)
91    COUNT,          // count x
92    ABOUT,          // about x
93    NEW,            // new <x> ()
94    DELETE_POINTER, // delete $aa
95    COPY,           // copy $aa
96    CLONE,          // clone $aa
97    ORIGIN,         // origin $aa
98    SUBSCRIBERS,    // subscribers $aa
99    PLAIN_SCOPE,    // scope xy;
100    // don't use 0x64 (magic number)
101
102    // comparators 0x80 - 0x8f
103    STRUCTURAL_EQUAL,     // ==
104    NOT_STRUCTURAL_EQUAL, // !=
105    EQUAL,                // ===
106    NOT_EQUAL,            // !==
107    GREATER,              // >
108    LESS,                 // <
109    GREATER_EQUAL,        // >=
110    LESS_EQUAL,           // <=
111    IS,                   // is
112
113    // logical + algebraic operators 0x90  - 0x9f
114    AND,       // &
115    OR,        // |
116    ADD,       // +
117    SUBTRACT,  // -
118    MULTIPLY,  // *
119    DIVIDE,    // /
120    NOT,       // !
121    MODULO,    // %
122    POWER,     // ^
123    INCREMENT, // ++
124    DECREMENT, // --
125
126    UNARY_PLUS,
127    UNARY_MINUS,
128    BITWISE_NOT, // ~
129
130    UNION, // | // TODO #428: maybe create a union collection of multiple values, instead of using this as a binary operator?
131
132    // assignment operators
133    ASSIGN,          // =
134    ADD_ASSIGN,      // +=
135    SUBTRACT_ASSIGN, // -=
136    MULTIPLY_ASSIGN, // *=
137    DIVIDE_ASSIGN,   // /=
138    MODULO_ASSIGN,   // %=
139    POWER_ASSIGN,    // ^=
140
141    // pointers & variables 0xa0 - 0xbf
142
143    // slots
144    // TODO #669: refactor with stack variable system?
145    GET_SLOT, // #xyz   0x0000-0x00ff = variables passed on between scopes, 0x0100-0xfdff = normal variables, 0xfe00-0xffff = it variables (#it.0, #it.1, ...) for function arguments
146    SET_SLOT, // #aa = ...
147    ALLOCATE_SLOT, // #aa = ...
148    SLOT_ACTION, // #x += ...
149    DROP_SLOT, // drop #aa
150
151    GET_INTERNAL_SLOT, // e.g. #endpoint
152
153    LABEL,        // $x
154    SET_LABEL,    // $x = ...,
155    INIT_LABEL,   // $x := ...
156    LABEL_ACTION, // $x += ...
157
158    // Note: fix to sync with RawPointerAddress
159    GET_REF = 120u8,          // $x
160    GET_INTERNAL_REF = 121u8, // $y, containing globally unique internal id
161    GET_LOCAL_REF = 122u8, // $x, containing only the id, origin id is inferred from sender
162    GET_OR_INIT_REF,       // $aa := ...
163    POINTER_ACTION,        // $aa += ...
164    CREATE_REF,            // &()
165    CREATE_REF_MUT,        // &mut ()
166    SET_REF,               // &aa = ...
167
168    SET_REFERENCE_VALUE, // *x = 10;
169
170    DEREF, // *x
171
172    CHILD_GET,           // .y
173    CHILD_SET,           // .y = a
174    CHILD_SET_REFERENCE, // .y $= a
175    CHILD_ACTION,        // .y += a, ...
176    CHILD_GET_REF,       // ->y
177
178    WILDCARD, // *
179
180    /// type byte codes --> switch to Type Space
181    TYPED_VALUE,
182    TYPE_EXPRESSION, // type()
183
184    // ...
185
186    // values 0xc0 - 0xdf
187    TEXT,
188    INT_8, // byte
189    INT_16,
190    INT_32,
191    INT_64,
192    INT_128,
193    INT_BIG,
194    INT, // default integer (unsized)
195
196    UINT_8, // u8
197    UINT_16,
198    UINT_32,
199    UINT_64,
200    UINT_128,
201
202    DECIMAL_F32,
203    DECIMAL_F64,
204    DECIMAL_BIG,
205    DECIMAL_AS_INT_32,
206    DECIMAL_AS_INT_16,
207
208    DECIMAL, // default decimal (unsized)
209
210    TRUE,
211    FALSE,
212    NULL,
213    VOID,
214    BUFFER,
215    QUANTITY,
216
217    SHORT_TEXT, // string with max. 255 characters
218
219    PERSON_ALIAS,
220    PERSON_ALIAS_WILDCARD,
221    INSTITUTION_ALIAS,
222    INSTITUTION_ALIAS_WILDCARD,
223    BOT,
224    BOT_WILDCARD,
225
226    ENDPOINT,
227    ENDPOINT_WILDCARD,
228
229    URL, //file://... , https://...
230
231    TIME, // ~2022-10-10~
232
233    // lists and maps 0xe0 - 0xef
234    LIST,       // (1,2,3)
235    SHORT_LIST, // (1,2,3) - optimized short list with up to 255 elements
236    MAP,        // (a:1, b:2)
237    SHORT_MAP,  // {a:1, b:2} - optimized short map with up to 255 elements
238
239    KEY_VALUE_SHORT_TEXT,
240    KEY_VALUE_DYNAMIC, // for object elements with dynamic key
241    KEY_PERMISSION,    // for object elements with permission prefix
242    INTERNAL_OBJECT_SLOT, // for object internal slots
243
244    // special instructions 0xf0 - 0xff
245    SYNC,      // <==
246    STOP_SYNC, // </=
247
248    STREAM,      // << stream
249    STOP_STREAM, // </ stream
250
251    REMOTE_EXECUTION, // ::
252
253    TRANSFORM,             // transform x <Int>
254    OBSERVE,               // observe x ()=>()
255    RUN,                   // run xy;
256    AWAIT,                 // await xy;
257    DEFER,                 // maybe xy;
258    FUNCTION,              // function ()
259    ASSERT,                // assert
260    ITERATOR,              // iterator ()
261    NEXT,                  // next it
262    FREEZE,                // freeze
263    SEAL,                  // seal
264    HAS,                   // x has y
265    KEYS,                  // keys x
266    GET_TYPE,              // type $aa
267    GET,                   // get file://..., get @user::34
268    RANGE,                 // ..
269    RESOLVE_RELATIVE_PATH, // ./abc
270    DO,                    // do xy;
271    DEFAULT,               // x default y
272    COLLAPSE,              // collapse x
273    RESPONSE,              // response x
274    CLONE_COLLAPSE,        // collapse
275}
276
277#[cfg(test)]
278mod tests {
279    use super::*;
280    use strum::IntoEnumIterator;
281
282    #[ignore]
283    #[test]
284    fn test_instruction_code_values() {
285        // print a list of all instruction codes and their values for debugging purposes
286        for code in InstructionCode::iter() {
287            println!("{:?} = {:2X}", code, code as u8);
288        }
289    }
290}