1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use crate::*;
use Instruction::*;
pub struct SqlError {
pub rname: String,
pub line: usize,
pub column: usize,
pub msg: String,
}
pub enum TableExpression {
Base(ObjRef),
Values(Vec<Vec<Expr>>),
}
#[derive(Clone, Copy)]
pub enum AssignOp {
Assign,
Append,
}
pub type Assigns = Vec<(usize, AssignOp)>;
pub struct SelectExpression {
pub colnames: Vec<String>,
pub assigns: Assigns,
pub exps: Vec<Expr>,
pub from: Option<Box<TableExpression>>,
pub wher: Option<Expr>,
pub orderby: Vec<(Expr, bool)>,
}
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub enum Token {
Less,
LessEqual,
GreaterEqual,
Greater,
Equal,
NotEqual,
In,
Plus,
Minus,
Times,
Divide,
Percent,
VBar,
And,
Or,
VBarEqual,
Id,
Number,
Hex,
String,
LBra,
RBra,
Comma,
Colon,
Dot,
Exclamation,
Unknown,
EndOfFile,
}
impl Token {
pub fn precedence(self) -> i8 {
const PA: [i8; 15] = [10, 10, 10, 10, 10, 10, 10, 20, 20, 30, 30, 30, 15, 8, 5];
PA[self as usize]
}
}
pub struct Expr {
pub exp: ExprIs,
pub data_type: DataType,
pub is_constant: bool,
pub checked: bool,
pub col: usize,
}
impl Expr {
pub fn new(exp: ExprIs) -> Self {
Expr {
exp,
data_type: NONE,
is_constant: false,
checked: false,
col: 0,
}
}
}
pub enum ExprIs {
Const(Value),
Local(usize),
ColName(String),
Binary(Token, Box<Expr>, Box<Expr>),
Not(Box<Expr>),
Minus(Box<Expr>),
Case(Vec<(Expr, Expr)>, Box<Expr>),
FuncCall(ObjRef, Vec<Expr>),
BuiltinCall(String, Vec<Expr>),
ScalarSelect(Box<SelectExpression>),
List(Vec<Expr>),
}
#[derive(PartialEq, PartialOrd, Eq, Hash, Clone)]
pub struct ObjRef {
pub schema: String,
pub name: String,
}
impl ObjRef {
pub fn new(s: &str, n: &str) -> Self {
Self {
schema: s.to_string(),
name: n.to_string(),
}
}
pub fn str(&self) -> String {
format!("[{}].[{}]", &self.schema, &self.name)
}
}
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub enum DataKind {
None = 0,
Binary = 1,
String = 2,
Int = 3,
Float = 4,
Bool = 5,
}
pub type DataType = usize;
const KBITS: usize = 3;
pub const NONE: DataType = DataKind::None as usize;
pub const BINARY: DataType = DataKind::Binary as usize + (16 << KBITS);
pub const STRING: DataType = DataKind::String as usize + (16 << KBITS);
pub const BIGINT: DataType = DataKind::Int as usize + (8 << KBITS);
pub const INT: DataType = DataKind::Int as usize + (4 << KBITS);
pub const SMALLINT: DataType = DataKind::Int as usize + (2 << KBITS);
pub const TINYINT: DataType = DataKind::Int as usize + (1 << KBITS);
pub const FLOAT: DataType = DataKind::Float as usize + (4 << KBITS);
pub const DOUBLE: DataType = DataKind::Float as usize + (8 << KBITS);
pub const BOOL: DataType = DataKind::Bool as usize + (1 << KBITS);
pub fn data_kind(x: DataType) -> DataKind {
const DKLOOK: [DataKind; 6] = [
DataKind::None,
DataKind::Binary,
DataKind::String,
DataKind::Int,
DataKind::Float,
DataKind::Bool,
];
DKLOOK[x % (1 << KBITS)]
}
#[must_use]
pub fn data_size(x: DataType) -> usize {
(x >> KBITS) & 31
}
pub struct Block<'a> {
pub param_count: usize,
pub return_type: DataType,
pub local_typ: Vec<DataType>,
pub ilist: Vec<Instruction>,
pub break_id: usize,
pub db: DB,
pub from: Option<CTableExpression>,
pub parse_only: bool,
jumps: Vec<usize>,
labels: HashMap<&'a [u8], usize>,
local_map: HashMap<&'a [u8], usize>,
locals: Vec<&'a [u8]>,
}
impl<'a> Block<'a> {
pub fn new(db: DB) -> Self {
Block {
ilist: Vec::new(),
jumps: Vec::new(),
labels: HashMap::new(),
local_map: HashMap::new(),
locals: Vec::new(),
local_typ: Vec::new(),
break_id: 0,
param_count: 0,
return_type: NONE,
from: None,
db,
parse_only: false,
}
}
pub fn resolve_jumps(&mut self) {
for (k, v) in &self.labels {
if self.jumps[*v] == usize::MAX {
panic!("Undefined label: {}", parse::tos(k));
}
}
for i in &mut self.ilist {
match i {
JumpIfFalse(x, _) | Jump(x) | ForNext(x, _) | ForSortNext(x, _) => {
*x = self.jumps[*x]
}
_ => {}
}
}
}
pub fn add(&mut self, s: Instruction) {
if !self.parse_only {
self.ilist.push(s);
}
}
pub fn dop(&mut self, dop: DO) {
if !self.parse_only {
self.add(DataOp(Box::new(dop)));
}
}
pub fn check_types(&self, r: &FunctionPtr, pkinds: &[DataKind]) {
if pkinds.len() != r.param_count {
panic!("param count mismatch");
}
for (i, pk) in pkinds.iter().enumerate() {
let ft = data_kind(r.local_typ[i]);
let et = *pk;
if ft != et {
panic!("param type mismatch expected {:?} got {:?}", ft, et);
}
}
}
pub fn def_local(&mut self, name: &'a [u8], dt: DataType) {
let local_id = self.local_typ.len();
self.local_typ.push(dt);
self.locals.push(name);
if self.local_map.contains_key(name) {
panic!("Duplicate variable name");
}
self.local_map.insert(name, local_id);
}
pub fn get_local(&self, name: &[u8]) -> Option<&usize> {
self.local_map.get(name)
}
pub fn local_name(&self, num: usize) -> &[u8] {
self.locals[num]
}
pub fn get_jump_id(&mut self) -> usize {
let result = self.jumps.len();
self.jumps.push(usize::MAX);
result
}
pub fn set_jump(&mut self, jump_id: usize) {
self.jumps[jump_id] = self.ilist.len();
}
pub fn get_loop_id(&mut self) -> usize {
let result = self.get_jump_id();
self.set_jump(result);
result
}
pub fn get_goto_label(&mut self, s: &'a [u8]) -> usize {
if let Some(jump_id) = self.labels.get(s) {
*jump_id
} else {
let jump_id = self.get_jump_id();
self.labels.insert(s, jump_id);
jump_id
}
}
pub fn set_goto_label(&mut self, s: &'a [u8]) {
if let Some(jump_id) = self.labels.get(s) {
let j = *jump_id;
if self.jumps[j] != usize::MAX {
panic!("Label already set");
}
self.set_jump(j);
} else {
let jump_id = self.get_loop_id();
self.labels.insert(s, jump_id);
}
}
pub fn kind(&self, e: &mut Expr) -> DataKind {
compile::c_check(self, e);
data_kind(e.data_type)
}
}