Skip to main content

reifydb_rql/
instruction.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::{
5	fragment::Fragment,
6	value::{Value, constraint::TypeConstraint, r#type::Type},
7};
8
9use crate::{
10	nodes::{
11		AlterFlowNode, AlterSequenceNode, AlterTableNode, AlterViewNode, CreateDeferredViewNode,
12		CreateDictionaryNode, CreateFlowNode, CreateNamespaceNode, CreateRingBufferNode,
13		CreateSubscriptionNode, CreateSumTypeNode, CreateTableNode, CreateTransactionalViewNode,
14		DeleteRingBufferNode, DeleteTableNode, FunctionParameter, InsertDictionaryNode, InsertRingBufferNode,
15		InsertTableNode, UpdateRingBufferNode, UpdateTableNode,
16	},
17	query::QueryPlan,
18};
19
20/// Address in the instruction stream (for jumps)
21pub type Addr = usize;
22
23/// A compiled user-defined function with pre-compiled body instructions
24#[derive(Debug, Clone)]
25pub struct CompiledFunctionDef {
26	/// Function name
27	pub name: Fragment,
28	/// Function parameters
29	pub parameters: Vec<FunctionParameter>,
30	/// Optional return type constraint
31	pub return_type: Option<TypeConstraint>,
32	/// Pre-compiled function body instructions
33	pub body: Vec<Instruction>,
34}
35
36/// Different types of scopes for variable management
37#[derive(Debug, Clone, PartialEq)]
38pub enum ScopeType {
39	/// Global scope (cannot be exited)
40	Global,
41	/// Function scope
42	Function,
43	/// Block scope
44	Block,
45	/// Conditional scope (if/else)
46	Conditional,
47	/// Loop scope
48	Loop,
49}
50
51#[derive(Debug, Clone)]
52pub enum Instruction {
53	// === Stack ===
54	PushConst(Value),
55	PushNone,
56	Pop,
57	Dup,
58
59	// === Variables ===
60	LoadVar(Fragment),
61	StoreVar(Fragment),
62	DeclareVar(Fragment),
63
64	// === Arithmetic (pop 2, push 1) ===
65	Add,
66	Sub,
67	Mul,
68	Div,
69	Rem,
70
71	// === Unary ===
72	Negate,
73	LogicNot,
74
75	// === Comparison (pop 2, push Boolean) ===
76	CmpEq,
77	CmpNe,
78	CmpLt,
79	CmpLe,
80	CmpGt,
81	CmpGe,
82
83	// === Logic ===
84	LogicAnd,
85	LogicOr,
86	LogicXor,
87
88	// === Compound ===
89	Between,
90	InList {
91		count: u16,
92		negated: bool,
93	},
94	Cast(Type),
95
96	// === Control flow ===
97	Jump(Addr),
98	JumpIfFalsePop(Addr),
99	JumpIfTruePop(Addr),
100	EnterScope(ScopeType),
101	ExitScope,
102	Break {
103		exit_scopes: usize,
104		addr: Addr,
105	},
106	Continue {
107		exit_scopes: usize,
108		addr: Addr,
109	},
110
111	// === Loops ===
112	ForInit {
113		variable_name: Fragment,
114	},
115	ForNext {
116		variable_name: Fragment,
117		addr: Addr,
118	},
119
120	// === Functions ===
121	DefineFunction(CompiledFunctionDef),
122	Call {
123		name: Fragment,
124		arity: u8,
125	},
126	ReturnValue,
127	ReturnVoid,
128
129	// === Query (volcano model) ===
130	Query(QueryPlan),
131
132	// === DDL ===
133	CreateNamespace(CreateNamespaceNode),
134	CreateTable(CreateTableNode),
135	CreateRingBuffer(CreateRingBufferNode),
136	CreateFlow(CreateFlowNode),
137	CreateDeferredView(CreateDeferredViewNode),
138	CreateTransactionalView(CreateTransactionalViewNode),
139	CreateDictionary(CreateDictionaryNode),
140	CreateSumType(CreateSumTypeNode),
141	CreateSubscription(CreateSubscriptionNode),
142	AlterSequence(AlterSequenceNode),
143	AlterTable(AlterTableNode),
144	AlterView(AlterViewNode),
145	AlterFlow(AlterFlowNode),
146
147	// === DML ===
148	Delete(DeleteTableNode),
149	DeleteRingBuffer(DeleteRingBufferNode),
150	InsertTable(InsertTableNode),
151	InsertRingBuffer(InsertRingBufferNode),
152	InsertDictionary(InsertDictionaryNode),
153	Update(UpdateTableNode),
154	UpdateRingBuffer(UpdateRingBufferNode),
155
156	// === Append ===
157	Append {
158		target: Fragment,
159	},
160
161	// === Output ===
162	Emit,
163
164	// === Control ===
165	Nop,
166	Halt,
167}