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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*! Intermediate code representation. */
use super::*;
use crate::reader::Offset;
use crate::utils;
use crate::{Object, RefValue};
#[derive(Debug, Clone)]
pub(in crate::compiler) enum ImlOp {
Nop, // Empty operation
Op(Op), // VM Operation
Load {
offset: Option<Offset>,
target: ImlValue,
//copy: bool, //enforce copy (Op::Sep)
},
Call {
offset: Option<Offset>,
target: ImlValue,
args: Option<(usize, bool)>,
},
// Alternation (Block) of sequences or ops
Alt {
alts: Vec<ImlOp>,
},
// Sequence of ops, optionally a collection
Seq {
seq: Vec<ImlOp>,
collect: bool, // Run a Context::collect() on successfull sequence match
},
// Conditional block
If {
peek: bool, // Peek test value instead of pop (required to implement the or-operator)
test: bool, // Boolean value to test against (true or false)
then: Box<ImlOp>, // Conditional code path
else_: Box<ImlOp>, // Optional code path executed otherwise
},
// Loop construct
Loop {
use_iterator: bool, // Test condition either for void (=true) or bool (=false)
initial: Box<ImlOp>, // Initialization
condition: Box<ImlOp>, // Abort condition
body: Box<ImlOp>, // Iterating body
},
}
impl ImlOp {
/// Creates a sequence from items, and optimizes stacked, unframed sequences
pub fn seq(items: Vec<ImlOp>, collect: bool) -> ImlOp {
let mut seq = Vec::new();
for item in items {
match item {
ImlOp::Nop => {}
ImlOp::Seq {
collect: false,
seq: items,
} => seq.extend(items),
item => seq.push(item),
}
}
match seq.len() {
0 => ImlOp::Nop,
1 if !collect => seq.pop().unwrap(),
_ => ImlOp::Seq { seq, collect },
}
}
/// Load value; This is only a shortcut for creating an ImlOp::Load{}
pub fn load(_scope: &Scope, offset: Option<Offset>, target: ImlValue) -> ImlOp {
// FIXME: Currently, doing `target: target.resolve(scope)` here will produce two usages!
ImlOp::Load { offset, target }
}
/// Load unknown value by name
pub fn load_by_name(scope: &Scope, offset: Option<Offset>, name: String) -> ImlOp {
let value = ImlValue::Name { offset, name }.try_resolve(scope);
Self::load(scope, offset.clone(), value)
}
/// Call known value
pub fn call(
scope: &Scope,
offset: Option<Offset>,
target: ImlValue,
args: Option<(usize, bool)>,
) -> ImlOp {
let target = target.try_resolve(scope);
// When args is unset, and the value is not callable without arguments,
// consider this call is a load.
if args.is_none() && !target.is_callable(true) {
// Currently not planned as final
return Self::load(scope, offset, target);
}
if target.is_consuming() {
scope.parselet().borrow().model.borrow_mut().is_consuming = true;
}
ImlOp::Call {
offset,
target,
args,
}
}
/// Call unknown value by name
pub fn call_by_name(
scope: &Scope,
offset: Option<Offset>,
name: String,
args: Option<(usize, bool)>,
) -> ImlOp {
// Perform early consumable detection depending on identifier's name
if utils::identifier_is_consumable(&name) {
scope.parselet().borrow().model.borrow_mut().is_consuming = true;
}
ImlOp::Call {
offset: offset.clone(),
target: ImlValue::Name { offset, name }.try_resolve(scope),
args,
}
}
/// Compile ImlOp construct into Op instructions of the resulting Tokay VM program
pub fn compile_to_vec(
&self,
program: &mut ImlProgram,
current: (&ImlRefParselet, usize),
) -> Vec<Op> {
let mut ops = Vec::new();
self.compile(program, current, &mut ops);
ops
}
/// Compile ImlOp construct into Op instructions of the resulting Tokay VM program
pub fn compile(
&self,
program: &mut ImlProgram,
current: (&ImlRefParselet, usize),
ops: &mut Vec<Op>,
) -> usize {
let start = ops.len();
match self {
ImlOp::Nop => {}
ImlOp::Op(op) => ops.push(op.clone()),
ImlOp::Load { offset, target } => {
target.compile(program, current, &offset, None, ops);
}
ImlOp::Call {
offset,
target,
args,
} => {
target.compile(program, current, &offset, Some(*args), ops);
}
ImlOp::Alt { alts } => {
let mut ret = Vec::new();
let mut iter = alts.iter();
let mut jumps = Vec::new();
let mut initial_fuse = None;
while let Some(item) = iter.next() {
let alt = item.compile_to_vec(program, current);
// When branch has more than one item, Frame it.
if iter.len() > 0 {
let consuming = item.is_consuming();
let fuse = alt.len() + if consuming { 3 } else { 2 };
if initial_fuse.is_none() {
initial_fuse = Some(fuse) // this is used for the initial frame
} else {
ret.push(Op::Fuse(fuse)); // this updates the fuse of the frame
}
ret.extend(alt);
if consuming {
// Insert Nop as location for later jump backpatch
ret.push(Op::Nop);
jumps.push(ret.len() - 1);
ret.push(Op::Reset);
} else {
ret.push(Op::ResetCapture);
}
} else {
ret.extend(alt);
}
}
// Backpatch remembered jumps
while let Some(addr) = jumps.pop() {
ret[addr] = Op::ForwardIfConsumed(ret.len() - addr);
}
// Wrap the entire body in its own frame when more than 1 alternative exists
if let Some(fuse) = initial_fuse {
ret.insert(0, Op::Frame(fuse));
ret.push(Op::Close);
}
ops.extend(ret);
}
ImlOp::Seq { seq, collect } => {
for item in seq.iter() {
item.compile(program, current, ops);
}
// Check if the sequence exists of more than one operational instruction
if *collect {
ops.insert(start, Op::Frame(0));
ops.push(Op::Collect);
ops.push(Op::Close);
}
}
ImlOp::If {
peek,
test,
then: then_part,
else_: else_part,
} => {
// Copy on peek
if *peek {
ops.push(Op::Copy(1));
}
let backpatch = ops.len();
ops.push(Op::Nop); // Backpatch operation placeholder
if *peek {
ops.push(Op::Drop)
}
// Then-part
let mut jump = then_part.compile(program, current, ops) + 1;
if !*peek {
let mut else_ops = Vec::new();
// Else-part
if else_part.compile(program, current, &mut else_ops) > 0 {
ops.push(Op::Forward(else_ops.len() + 1));
jump += 1;
ops.extend(else_ops);
}
} else {
jump += 1;
}
// Insert the final condition and its failure target.
if *test {
ops[backpatch] = Op::ForwardIfFalse(jump);
} else {
ops[backpatch] = Op::ForwardIfTrue(jump);
}
}
ImlOp::Loop {
use_iterator,
initial,
condition,
body,
} => {
let consuming: Option<bool> = None; // fixme: Currently not sure if this is an issue.
let mut repeat = Vec::new();
initial.compile(program, current, ops);
if condition.compile(program, current, &mut repeat) > 0 {
if *use_iterator {
repeat.push(Op::ForwardIfNotVoid(2));
} else {
repeat.push(Op::ForwardIfTrue(2));
}
repeat.push(Op::Break);
}
body.compile(program, current, &mut repeat);
let len = repeat.len() + if consuming.is_some() { 3 } else { 2 };
ops.push(Op::Loop(len));
// fixme: consuming flag must be handled differently.
if consuming.is_some() {
ops.push(Op::Fuse(ops.len() - start + 2));
}
ops.extend(repeat);
ops.push(Op::Continue);
if consuming.is_some() {
ops.push(Op::Break);
}
}
}
ops.len() - start
}
// Defines the ImlOp's consuming state from point of view as an ImlOp.
// The ImlOp deeply can still consume, but this is a semantic issue.
// During code-generation, this function is useful to determine whether
// the ImlOp is directly consuming or not.
pub fn is_consuming(&self) -> bool {
fn walk(op: &ImlOp) -> Option<bool> {
// Query along ImlOp structure
match op {
ImlOp::Call { target, .. } => {
if target.is_consuming() {
return Some(true);
}
None
}
ImlOp::Op(Op::Next) => Some(true),
ImlOp::Loop { .. } | ImlOp::If { peek: false, .. } => Some(false),
ImlOp::Alt { alts: items } | ImlOp::Seq { seq: items, .. } => {
for item in items {
if let Some(res) = walk(item) {
return Some(res);
}
}
None
}
ImlOp::If { then, else_, .. } => {
for item in [&then, &else_] {
if let Some(res) = walk(item) {
return Some(res);
}
}
None
}
_ => None,
}
}
walk(self).unwrap_or(false)
}
/** Returns a value to operate with or evaluate during compile-time.
The function will only return Ok(Value) when the static_expression_evaluation-feature
is enabled, it is ImlOp::Load and the value is NOT a callable! */
pub fn get_evaluable_value(&self) -> Result<RefValue, ()> {
if cfg!(feature = "static_expression_evaluation") {
if let Self::Load {
target: ImlValue::Value(value),
..
} = self
{
if !value.is_callable(true) {
return Ok(value.clone().into());
}
}
}
Err(())
}
}
impl From<Op> for ImlOp {
fn from(op: Op) -> Self {
ImlOp::Op(op)
}
}
impl From<Vec<ImlOp>> for ImlOp {
fn from(items: Vec<ImlOp>) -> Self {
ImlOp::seq(items, false)
}
}