1use crate::alloc::{LBox, LRc, LVec};
2use crate::{
3 Assigns, Block, Cell, ColInfo, DataType, EvalEnv, Expr, IndexInfo, ObjRef, PagePtr, Rc,
4 RefCell, Table, Value, panic,
5};
6
7#[non_exhaustive]
9pub enum Instruction {
10 PushConst(Value),
12 PushValue(CExpPtr<Value>),
14 PushLocal(usize),
16 PopToLocal(usize),
18 Jump(usize),
20 JumpIfFalse(usize, CExpPtr<bool>),
22 Call(LRc<Function>),
24 Return,
26 Throw,
28 Execute,
30 ForInit(usize, LBox<CTableExpression>),
32 ForNext(usize, LBox<ForNextInfo>),
34 ForSortInit(usize, LBox<CFromExpression>),
36 ForSortNext(usize, LBox<(usize, usize, Assigns)>),
38 DataOp(LBox<DO>),
40 Select(LBox<CFromExpression>),
42 Set(LBox<CFromExpression>),
44 PushInt(CExpPtr<i64>),
47 PushFloat(CExpPtr<f64>),
49 PushBool(CExpPtr<bool>),
51 AssignLocal(usize, CExpPtr<Value>),
54 AppendLocal(usize, CExpPtr<Value>),
56 IncLocal(usize, CExpPtr<Value>),
58 DecLocal(usize, CExpPtr<Value>),
60}
61
62#[non_exhaustive]
64pub struct Function {
65 pub param_count: usize,
67 pub return_type: DataType,
69 pub local_typ: LVec<DataType>,
71 pub source: Rc<String>,
73 pub ilist: RefCell<LVec<Instruction>>, pub compiled: Cell<bool>,
77}
78
79pub trait CExp<T> {
81 fn eval(&self, ee: &mut EvalEnv, data: &[u8]) -> T;
83}
84
85#[cfg(feature = "dynbox")]
86pub type CExpPtr<T> = LBox<dyn CExp<T>>;
88
89#[cfg(not(feature = "dynbox"))]
90pub type CExpPtr<T> = Box<dyn CExp<T>>;
92
93#[derive(Clone, Copy)]
95#[non_exhaustive]
96pub enum CompileFunc {
97 Value(fn(&Block, &mut [Expr]) -> CExpPtr<Value>),
99 Int(fn(&Block, &mut [Expr]) -> CExpPtr<i64>),
101 Float(fn(&Block, &mut [Expr]) -> CExpPtr<f64>),
103}
104
105pub type DataSource = Box<dyn Iterator<Item = (PagePtr, usize)>>;
107
108#[non_exhaustive]
110pub struct ForState {
111 pub data_source: DataSource,
113}
114impl std::fmt::Debug for ForState {
115 fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
116 Ok(())
117 }
118}
119
120#[non_exhaustive]
122pub struct ForSortState {
123 pub ix: usize,
125 pub rows: Vec<Vec<Value>>,
127}
128impl std::fmt::Debug for ForSortState {
129 fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
130 Ok(())
131 }
132}
133
134#[non_exhaustive]
136pub struct ForNextInfo {
137 pub for_id: usize,
139 pub assigns: Assigns,
141 pub exps: LVec<CExpPtr<Value>>,
143 pub wher: Option<CExpPtr<bool>>,
145}
146
147#[non_exhaustive]
149pub enum CTableExpression {
150 Base(LRc<Table>),
152 IdGet(LRc<Table>, CExpPtr<i64>),
154 IxGet(LRc<Table>, LVec<CExpPtr<Value>>, usize),
156 Values(LVec<LVec<CExpPtr<Value>>>),
158}
159
160impl CTableExpression {
161 pub fn table(&self) -> LRc<Table> {
163 match self {
164 CTableExpression::Base(t) => t.clone(),
165 CTableExpression::IdGet(t, _) => t.clone(),
166 CTableExpression::IxGet(t, _, _) => t.clone(),
167 _ => panic!(),
168 }
169 }
170}
171
172#[non_exhaustive]
174pub struct CFromExpression {
175 pub colnames: LVec<LBox<str>>,
177 pub assigns: Assigns,
179 pub exps: LVec<CExpPtr<Value>>,
181 pub from: Option<CTableExpression>,
183 pub wher: Option<CExpPtr<bool>>,
185 pub orderby: LVec<CExpPtr<Value>>,
187 pub desc: LVec<bool>,
189}
190
191#[non_exhaustive]
193pub enum DO {
194 CreateSchema(LBox<str>),
196 CreateTable(ColInfo),
198 CreateIndex(IndexInfo),
200 CreateFunction(ObjRef, Rc<String>, bool),
202 AlterTable(ObjRef, LVec<AlterCol>),
204 DropSchema(LBox<str>),
206 DropTable(ObjRef),
208 DropIndex(ObjRef, LBox<str>),
210 DropFunction(ObjRef),
212 Insert(LRc<Table>, LVec<usize>, CTableExpression),
214 Update(
216 LVec<(usize, CExpPtr<Value>)>,
217 CTableExpression,
218 Option<CExpPtr<bool>>,
219 ),
220 Delete(CTableExpression, Option<CExpPtr<bool>>),
222}
223
224#[non_exhaustive]
226pub enum AlterCol {
227 Add(LBox<str>, DataType),
229 Drop(LBox<str>),
231 Modify(LBox<str>, DataType),
233}