treescript-interpreter 0.1.1

TreeScript interpreter, with some built-in libraries and API to create more
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
extern crate enum_map;
extern crate serde;
extern crate serde_repr;
use crate::session::Session;
use crate::value::{Prim, Value};
use enum_map::{Enum, EnumMap};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::collections::{HashSet, VecDeque};
use std::iter::FromIterator;
use std::ops::{Index, IndexMut};
use std::rc::Weak;

const MAX_NUM_BINDS: usize = 32;

struct BindFrame {
  local: [Option<Value>; MAX_NUM_BINDS],
  parent_idxs: HashSet<usize>,
}

struct BindStack(VecDeque<BindFrame>);

#[derive(Clone, Copy, Debug, Deserialize, Enum, Eq, PartialEq, Serialize)]
pub enum ReduceType {
  Regular,
  EvalCtx,
  AltConsume,
}

pub type ReduceResult = bool;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Consume {
  Bind(usize),
  Prim(Prim),
  Record(String),
  Function { head: String, props: Vec<Value> },
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GroupRef {
  pub idx: usize,
  pub props: Vec<Value>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReducerClause {
  pub consumes: Vec<Consume>,
  pub produce: Value,
  pub groups: Vec<GroupRef>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Reducer {
  pub input: ReducerClause,
  pub output: ReducerClause,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Statement {
  Reducer(Reducer),
  Group(GroupRef),
}

#[derive(Clone, Debug, Deserialize_repr, Serialize_repr)]
#[repr(usize)]
pub enum GroupMode {
  Continue,
  Stop,
  Loop,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GroupDefSerial {
  pub props: Vec<usize>,
  pub mode: GroupMode,
  pub statements: Vec<Vec<Statement>>,
}

#[derive(Clone, Debug)]
pub struct GroupDef {
  pub props: Vec<usize>,
  pub mode: GroupMode,
  pub statements: EnumMap<ReduceType, Vec<Statement>>,
  pub env: Weak<Vec<GroupDef>>,
}

impl BindFrame {
  fn new<'a, I: IntoIterator<Item = &'a usize>>(parent_idxs: I) -> BindFrame {
    return BindFrame {
      local: Default::default(),
      parent_idxs: HashSet::from_iter(parent_idxs.into_iter().map(|idx| *idx)),
    };
  }
}

impl Index<usize> for BindStack {
  type Output = Option<Value>;

  fn index(&self, idx: usize) -> &Option<Value> {
    if idx == 0 {
      panic!("can't reference bind 0")
    }
    for frame in self.0.iter() {
      if !frame.parent_idxs.contains(&idx) {
        return &frame.local[idx - 1];
      }
      dprint!("PRINT_FRAME", "index {} fell\n", idx);
    }
    panic!("root (undefined) index: {}", idx);
  }
}

impl IndexMut<usize> for BindStack {
  fn index_mut(&mut self, idx: usize) -> &mut Option<Value> {
    if idx == 0 {
      panic!("can't reference bind 0")
    }
    dprint!("PRINT_FRAME", "index_mut: {}", idx);
    for frame in self.0.iter_mut() {
      if !frame.parent_idxs.contains(&idx) {
        dprint!("PRINT_FRAME", "\n");
        return &mut frame.local[idx - 1];
      }
      dprint!("PRINT_FRAME", "+");
    }
    panic!("root (undefined) index: {}", idx);
  }
}

impl BindStack {
  fn root() -> BindStack {
    return BindStack(VecDeque::new());
  }

  fn push<'a, I: IntoIterator<Item = &'a usize>>(&mut self, parent_idxs: I) {
    self.0.push_front(BindFrame::new(parent_idxs));
    dprint!(
      "PRINT_FRAME",
      "pushing: {:?}\n",
      self.0.front().unwrap().parent_idxs
    );
  }

  fn pop(&mut self) {
    dprint!(
      "PRINT_FRAME",
      "popping: {:?}\n",
      self.0.front().unwrap().parent_idxs
    );
    self.0.pop_front().expect("no frames left to pop");
  }
}

impl Consume {
  fn seq_from(value: Value) -> Vec<Consume> {
    match value {
      Value::Splice(idx) => vec![Consume::Bind(idx)],
      Value::Prim(prim) => vec![Consume::Prim(prim)],
      Value::Record { head, props } => {
        let mut res = vec![Consume::Record(head)];
        for prop in props {
          res.append(&mut Consume::seq_from(prop));
        }
        return res;
      }
    }
  }

  fn subst_bind(self, old: usize, new: &Value) -> Vec<Consume> {
    match self {
      Consume::Bind(idx) if idx == old => return Consume::seq_from(new.clone()),
      _ => return vec![self],
    }
  }
}

impl GroupRef {
  fn subst_bind(&mut self, old: usize, new: &Value) {
    for prop in self.props.iter_mut() {
      prop.subst(&Value::Splice(old), new);
    }
  }
}

impl ReducerClause {
  fn subst_bind(&mut self, old: usize, new: &Value) {
    self.consumes = self
      .consumes
      .iter()
      .flat_map(|consume| consume.clone().subst_bind(old, new))
      .collect();
    self.produce.subst(&Value::Splice(old), new);
    for group_ref in self.groups.iter_mut() {
      group_ref.subst_bind(old, new);
    }
  }
}

impl Reducer {
  fn subst_bind(&mut self, old: usize, new: &Value) {
    self.input.subst_bind(old, new);
    self.output.subst_bind(old, new);
  }
}

impl Statement {
  fn subst_bind(&mut self, old: usize, new: &Value) {
    match self {
      Statement::Reducer(reducer) => reducer.subst_bind(old, new),
      Statement::Group(group_ref) => group_ref.subst_bind(old, new),
    }
  }
}

impl GroupDef {
  pub fn from_no_env(x: GroupDefSerial) -> GroupDef {
    let stmts = x.statements;
    return GroupDef {
      props: x.props,
      mode: x.mode,
      statements: EnumMap::from(|typ| stmts[typ as usize].clone()),
      env: Weak::new(),
    };
  }

  fn subst_bind(&mut self, old: usize, new: &Value) {
    for (_, stmts) in self.statements.iter_mut() {
      for stmt in stmts {
        stmt.subst_bind(old, new);
      }
    }
  }

  fn subst_props<I: IntoIterator<Item = Value>>(&mut self, in_props: I) {
    let old_props = self.props.clone();
    self.props = Vec::new();
    for (prop_idx, in_prop) in Iterator::zip(old_props.into_iter(), in_props) {
      let mut in_prop_idxs = in_prop.sub_splices();
      self.props.append(&mut in_prop_idxs);
      self.subst_bind(prop_idx, &in_prop)
    }
  }

  fn resolve<F: FnMut(&Value) -> Value>(
    &self,
    group_ref: &GroupRef,
    prop_transformer: F,
  ) -> GroupDef {
    let mut group = self.env.upgrade().unwrap()[group_ref.idx].clone();
    dprint!("PRINT_REDUCE", "Resolving {:?}\n", group_ref.idx);
    let props = group_ref.props.iter().map(prop_transformer);
    group.subst_props(props);
    return group;
  }

  fn apply_if_function(&self, session: &mut Session, x: &mut Value) {
    if let Value::Record { head, props } = x {
      if head == "#Flush" {
        *x = props[0].clone();
        x.flush();
      } else if head.starts_with("#") {
        let head = String::from(&head[1..]);
        *x = session.call_fun_val(head, props.clone());
      }
    }
  }

  fn apply_functions(&self, session: &mut Session, x: &mut Value) {
    if let Value::Record { head: _, props } = x {
      for sub in props {
        self.apply_functions(session, sub);
      }
    }
    self.apply_if_function(session, x);
  }

  fn apply_consume(
    &self,
    session: &mut Session,
    stack: &mut Vec<Value>,
    binds: &mut BindStack,
    consume: &Consume,
  ) -> ReduceResult {
    let consumed = stack.pop().unwrap();
    match consume.clone() {
      Consume::Prim(in_prim) => {
        return consumed == Value::Prim(in_prim);
      }
      Consume::Record(in_head) => {
        if let Value::Record {
          head: consumed_head,
          props: mut consumed_props,
        } = consumed
        {
          if consumed_head == in_head {
            consumed_props.reverse();
            stack.append(&mut consumed_props);
            return true;
          } else {
            return false;
          }
        } else {
          return false;
        }
      }
      Consume::Bind(idx) => {
        if idx == 0 {
          return true;
        } else {
          let bind = binds.index_mut(idx);
          if bind == &Option::None {
            *bind = Option::Some(consumed);
            return true;
          } else if bind == &Option::Some(consumed) {
            return true;
          } else {
            return false;
          }
        }
      }
      Consume::Function { head, props } => {
        let mut out = session.call_fun_val(head, props);
        self.apply_functions(session, &mut out);
        return consumed == out;
      }
    }
  }

  fn apply_produce(&self, session: &mut Session, binds: &mut BindStack, produce: &Value) -> Value {
    let mut res = produce.clone();
    //Keep raw splices, don't error
    res.fill_splices(|idx| binds[idx].as_ref().cloned().unwrap_or(Value::Splice(idx)));
    self.apply_functions(session, &mut res);
    return res;
  }

  fn guard_clause(
    &self,
    session: &mut Session,
    binds: &mut BindStack,
    x: &Value,
    clause: &ReducerClause,
  ) -> ReduceResult {
    dprint!("PRINT_REDUCE", "Consuming {} ", clause.produce);
    let mut stack = vec![x.clone()];
    for consume in &clause.consumes {
      if !self.apply_consume(session, &mut stack, binds, &consume) {
        dprint!("PRINT_REDUCE", "- Failed\n");
        return false;
      }
    }
    for group_ref in &clause.groups {
      dprint!("PRINT_REDUCE", "+ Group");
      if !self
        .resolve(group_ref, |prop| self.apply_produce(session, binds, prop))
        .guard(session, binds)
      {
        dprint!("PRINT_REDUCE", "- Failed\n");
        return false;
      }
    }
    dprint!("PRINT_REDUCE", "- Succeeded\n");
    return true;
  }

  fn apply_clause(
    &self,
    session: &mut Session,
    binds: &mut BindStack,
    clause: &ReducerClause,
  ) -> Value {
    dprint!("PRINT_REDUCE", "Producing {}\n", clause.produce);
    let mut out = self.apply_produce(session, binds, &clause.produce);
    for group_ref in &clause.groups {
      self
        .resolve(group_ref, |prop| self.apply_produce(session, binds, prop))
        .reduce_nested(session, binds, ReduceType::Regular, &mut out);
    }
    return out;
  }

  fn guard_reducer(
    &self,
    session: &mut Session,
    binds: &mut BindStack,
    reducer: &Reducer,
  ) -> ReduceResult {
    let mut out = self.apply_produce(session, binds, &reducer.input.produce);
    if out.any_sub(|sub| sub.is_splice()) {
      return true;
    }
    for group_ref in &reducer.input.groups {
      self
        .resolve(group_ref, |prop| self.apply_produce(session, binds, prop))
        .reduce_nested(session, binds, ReduceType::Regular, &mut out);
    }
    binds.push(&self.props);
    let res = self.guard_clause(session, binds, &out, &reducer.output);
    binds.pop();
    return res;
  }

  fn apply_reducer(
    &self,
    session: &mut Session,
    in_binds: &mut BindStack,
    x: &mut Value,
    reducer: &Reducer,
  ) -> ReduceResult {
    in_binds.push(&self.props);
    if !self.guard_clause(session, in_binds, x, &reducer.input) {
      in_binds.pop();
      return false;
    }
    *x = self.apply_clause(session, in_binds, &reducer.output);
    in_binds.pop();
    return true;
  }

  fn apply_statement(
    &self,
    session: &mut Session,
    in_binds: &mut BindStack,
    reduce_type: ReduceType,
    x: &mut Value,
    statement: &Statement,
  ) -> ReduceResult {
    match statement {
      Statement::Reducer(reducer) => return self.apply_reducer(session, in_binds, x, reducer),
      Statement::Group(group_ref) => {
        //clone is probably unnecessary
        return self.resolve(group_ref, |prop| prop.clone()).reduce_nested(
          session,
          in_binds,
          reduce_type,
          x,
        );
      }
    }
  }

  fn guard_statement(
    &self,
    session: &mut Session,
    binds: &mut BindStack,
    statement: &Statement,
  ) -> ReduceResult {
    match statement {
      Statement::Reducer(reducer) => return self.guard_reducer(session, binds, reducer),
      Statement::Group(group_ref) => {
        //clone is probably unnecessary
        return self
          .resolve(group_ref, |prop| prop.clone())
          .guard(session, binds);
      }
    }
  }

  fn guard(&self, session: &mut Session, binds: &mut BindStack) -> ReduceResult {
    for stmt in &self.statements[ReduceType::Regular] {
      if !self.guard_statement(session, binds, &stmt) {
        return false;
      }
    }
    return true;
  }

  fn reduce_nested(
    &self,
    session: &mut Session,
    in_binds: &mut BindStack,
    reduce_type: ReduceType,
    x: &mut Value,
  ) -> ReduceResult {
    dprint!("PRINT_REDUCE", "Reducing {}\n", x);
    let mut progress = false;
    for stmt in self.statements[reduce_type].iter() {
      let mut reduce_res = self.apply_statement(session, in_binds, reduce_type, x, stmt);
      if !reduce_res {
        let mut alt_out = Value::Record {
          head: String::from("C"),
          props: vec![x.clone()],
        };
        for alt_out_stmt in &self.statements[ReduceType::AltConsume] {
          if self.apply_statement(session, in_binds, reduce_type, &mut alt_out, alt_out_stmt) {
            for alt_in in alt_out.breadth_first_mut() {
              if let Value::Record { head, props } = alt_in {
                if head == "C" {
                  *alt_in = props.first().unwrap().clone();
                  if self.apply_statement(session, in_binds, reduce_type, alt_in, stmt) {
                    reduce_res = true;
                  }
                  break;
                }
              }
            }
            if reduce_res {
              *x = alt_out;
              break;
            }
          }
        }
      }
      if reduce_res {
        progress = true;
        match self.mode {
          GroupMode::Continue => (),
          GroupMode::Stop => return true,
          GroupMode::Loop => {
            self.reduce_nested(session, in_binds, reduce_type, x);
            return true;
          }
        };
      }
    }
    if reduce_type == ReduceType::Regular {
      for alt_out_stmt in self.statements[ReduceType::EvalCtx].iter() {
        let mut alt_out = Value::Record {
          head: String::from("E"),
          props: vec![x.clone()],
        };
        if self.apply_statement(
          session,
          in_binds,
          ReduceType::EvalCtx,
          &mut alt_out,
          &alt_out_stmt,
        ) {
          alt_out.modify_children(&mut |alt_in| {
            if let Value::Record { head, props: p } = alt_in {
              if head == "E" {
                *alt_in = p.first().unwrap().clone();
                if self.reduce_nested(session, in_binds, reduce_type, alt_in) {
                  progress = true;
                }
                return false;
              }
            }
            return true;
          });
          if progress {
            *x = alt_out;
            match self.mode {
              GroupMode::Continue => (),
              GroupMode::Stop => return true,
              GroupMode::Loop => {
                self.reduce_nested(session, in_binds, reduce_type, x);
                return true;
              }
            };
            break;
          }
        }
      }
    }
    return progress;
  }

  pub fn reduce(&self, session: &mut Session, x: &mut Value) -> ReduceResult {
    assert!(
      self.props.is_empty(),
      "can't reduce as initial group, not the initial group, has properties"
    );
    return self.reduce_nested(session, &mut BindStack::root(), ReduceType::Regular, x);
  }
}