1use crate::{
2 panic, Assigns, Block, Cell, ColInfo, DataType, EvalEnv, Expr, IndexInfo, ObjRef, PagePtr, Rc,
3 RefCell, Table, Value,
4};
5
6#[non_exhaustive]
8pub enum Instruction {
9 PushConst(Value),
11 PushValue(CExpPtr<Value>),
13 PushLocal(usize),
15 PopToLocal(usize),
17 Jump(usize),
19 JumpIfFalse(usize, CExpPtr<bool>),
21 Call(Rc<Function>),
23 Return,
25 Throw,
27 Execute,
29 ForInit(usize, Box<CTableExpression>),
31 ForNext(usize, Box<ForNextInfo>),
33 ForSortInit(usize, Box<CFromExpression>),
35 ForSortNext(usize, Box<(usize, usize, Assigns)>),
37 DataOp(Box<DO>),
39 Select(Box<CFromExpression>),
41 Set(Box<CFromExpression>),
43 PushInt(CExpPtr<i64>),
46 PushFloat(CExpPtr<f64>),
48 PushBool(CExpPtr<bool>),
50 AssignLocal(usize, CExpPtr<Value>),
53 AppendLocal(usize, CExpPtr<Value>),
55 IncLocal(usize, CExpPtr<Value>),
57 DecLocal(usize, CExpPtr<Value>),
59}
60
61#[non_exhaustive]
63pub struct Function {
64 pub param_count: usize,
66 pub return_type: DataType,
68 pub local_typ: Vec<DataType>,
70 pub source: Rc<String>,
72 pub ilist: RefCell<Vec<Instruction>>, pub compiled: Cell<bool>,
76}
77
78pub trait CExp<T> {
80 fn eval(&self, ee: &mut EvalEnv, data: &[u8]) -> T;
82}
83
84pub type CExpPtr<T> = Box<dyn CExp<T>>;
86
87#[derive(Clone, Copy)]
89#[non_exhaustive]
90pub enum CompileFunc {
91 Value(fn(&Block, &mut [Expr]) -> CExpPtr<Value>),
93 Int(fn(&Block, &mut [Expr]) -> CExpPtr<i64>),
95 Float(fn(&Block, &mut [Expr]) -> CExpPtr<f64>),
97}
98
99pub type DataSource = Box<dyn Iterator<Item = (PagePtr, usize)>>;
101
102#[non_exhaustive]
104pub struct ForState {
105 pub data_source: DataSource,
107}
108impl std::fmt::Debug for ForState {
109 fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
110 Ok(())
111 }
112}
113
114#[non_exhaustive]
116pub struct ForSortState {
117 pub ix: usize,
119 pub rows: Vec<Vec<Value>>,
121}
122impl std::fmt::Debug for ForSortState {
123 fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
124 Ok(())
125 }
126}
127
128#[non_exhaustive]
130pub struct ForNextInfo {
131 pub for_id: usize,
133 pub assigns: Assigns,
135 pub exps: Vec<CExpPtr<Value>>,
137 pub wher: Option<CExpPtr<bool>>,
139}
140
141#[non_exhaustive]
143pub enum CTableExpression {
144 Base(Rc<Table>),
146 IdGet(Rc<Table>, CExpPtr<i64>),
148 IxGet(Rc<Table>, Vec<CExpPtr<Value>>, usize),
150 Values(Vec<Vec<CExpPtr<Value>>>),
152}
153
154impl CTableExpression {
155 pub fn table(&self) -> Rc<Table> {
157 match self {
158 CTableExpression::Base(t) => t.clone(),
159 CTableExpression::IdGet(t, _) => t.clone(),
160 CTableExpression::IxGet(t, _, _) => t.clone(),
161 _ => panic!(),
162 }
163 }
164}
165
166#[non_exhaustive]
168pub struct CFromExpression {
169 pub colnames: Vec<String>,
171 pub assigns: Assigns,
173 pub exps: Vec<CExpPtr<Value>>,
175 pub from: Option<CTableExpression>,
177 pub wher: Option<CExpPtr<bool>>,
179 pub orderby: Vec<CExpPtr<Value>>,
181 pub desc: Vec<bool>,
183}
184
185#[non_exhaustive]
187pub enum DO {
188 CreateSchema(String),
190 CreateTable(ColInfo),
192 CreateIndex(IndexInfo),
194 CreateFunction(ObjRef, Rc<String>, bool),
196 AlterTable(ObjRef, Vec<AlterCol>),
198 DropSchema(String),
200 DropTable(ObjRef),
202 DropIndex(ObjRef, String),
204 DropFunction(ObjRef),
206 Insert(Rc<Table>, Vec<usize>, CTableExpression),
208 Update(
210 Vec<(usize, CExpPtr<Value>)>,
211 CTableExpression,
212 Option<CExpPtr<bool>>,
213 ),
214 Delete(CTableExpression, Option<CExpPtr<bool>>),
216}
217
218#[non_exhaustive]
220pub enum AlterCol {
221 Add(String, DataType),
223 Drop(String),
225 Modify(String, DataType),
227}