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
use std;
use std::collections::HashMap;
use std::rc::Rc;

use super::{Command, Status};

#[derive(Debug)]
struct StackFrame {
  store_load: Vec<Status>, // TODO move out
  ops: *const Operation,
  len: usize,
  pos: usize,
}

struct FrameHolder<'a, 'b> where 'b: 'a {
  frame: Option<StackFrame>,
  _lt: std::marker::PhantomData<&'a [Operation]>,
  state: &'b mut State,
}

impl<'a, 'b> FrameHolder<'a, 'b> where 'b: 'a  {
  fn new(state: &'b mut State, ops: &'a [Operation]) -> FrameHolder<'a, 'b> where 'b: 'a {
    FrameHolder {
      frame: Some(StackFrame { store_load: Vec::new(), ops: ops.as_ptr(), len: ops.len(), pos: 0 }),
      _lt: std::marker::PhantomData,
      state: state
    }
  }

  fn with<F, R>(mut self, f: F) -> R where F: FnOnce(&mut State) -> R {
    let frame = self.frame.take().unwrap();
    self.state.frames.push(frame);
    f(self.state)
  }
}

impl<'a, 'b> Drop for FrameHolder<'a, 'b> where 'b: 'a  {
  fn drop(&mut self) {
    self.state.frames.pop();
  }
}

#[derive(Debug, Clone)]
pub struct Operation {
  trigger: [bool; 2],
  force: [bool; 2],
  cont: [bool; 4],
  op: String,
  args: Vec<String>
}

impl Operation {
  pub fn trigger(&self) -> [bool; 2] {
    self.trigger
  }
}

#[derive(Debug)]
pub struct State {
  frames: Vec<StackFrame>,
  commands: HashMap<&'static str, Rc<Command>>,
}

impl State {
  pub fn new() -> State {
    State { frames: Vec::new(), commands: HashMap::new() }
  }

  pub fn register_command<T: 'static + Command>(&mut self, command: T) {
    self.commands.insert(command.get_name(), Rc::new(command));
  }

  // TODO replace
  pub fn get_store_load<F, R>(&mut self, f: F) -> R where F: FnOnce(&mut Vec<Status>) -> R {
    f(&mut self.frames.last_mut().unwrap().store_load)
  }

  pub fn get_op<F, R>(&self, f: F) -> R where F: FnOnce(&Operation) -> R {
    unsafe {
      let frame = self.frames.last().unwrap();
      f(&std::slice::from_raw_parts(frame.ops, frame.len)[frame.pos])
    }
  }

  pub fn get_pos(&self) -> usize {
    self.frames.last().unwrap().pos
  }

  pub fn call(&mut self, prog: &[Operation]) -> Status {
    FrameHolder::new(self, prog).with(|x| x.run(0).no_return())
  }

  pub fn run(&mut self, from: usize) -> Status {
    let mut status = Status::Success;
    let oldpos = self.frames.last_mut().unwrap().pos;
    //let mut pos = from;
    self.frames.last_mut().unwrap().pos = from;
    while self.frames.last().unwrap().pos < self.frames.last().unwrap().len {
      status = if self.get_op(|op| op.trigger[*status]) {
        let newstatus = {
          if let Some(x) = self.get_op(|op| self.commands.get(op.op.as_str()).map(|x| x.clone())) {
            let args = self.get_op(|op| op.args.clone()); // TODO get rid of the clone
            x.run(self, status, &args)
          } else {
            Status::Failure
          }
        };
        if newstatus.is_return() {
          self.frames.last_mut().unwrap().pos = oldpos;
          return newstatus;
        }
        if self.get_op(|op| op.cont[*status * 2 + *newstatus]) {
          Status::Success
        } else {
          Status::Failure
        }
      } else if self.get_op(|op| op.force[*status]) {
        status
      } else {
        self.frames.last_mut().unwrap().pos = oldpos;
        return status;
      };
      self.frames.last_mut().unwrap().pos += 1;
    }
    self.frames.last_mut().unwrap().pos = oldpos;
    status
  }

  pub fn parse<I>(&mut self, program: I) -> Vec<Operation> where I: Iterator<Item=char> {
    program.chain("\n".chars()).scan(String::new(), |state, x| {
      if x == '\r' || x == '\n' {
        let s = state.clone();
        state.clear();
        Some(Some(s))
      } else {
        state.push(x);
        Some(None)
      }
    }).flat_map(|x| x) // flatten
      .filter(|x| !x.is_empty())
      .filter(|x| !x.starts_with("#"))
      .map(|x| {
        let mut itr = x.chars();
        let trigger = itr.next().and_then(|c| c.to_digit(16)).unwrap();
        let cont = itr.next().and_then(|c| c.to_digit(16)).unwrap();
        if itr.next().unwrap() != ' ' { panic!(); }
        let mut parts = itr.as_str().split(' ');
        let op = parts.next().unwrap().to_owned();
        let args = parts.map(|x| x.to_owned()).collect();
        Operation {
          trigger: [trigger & 1 != 0, trigger & 2 != 0],
          force: [trigger & 4 != 0, trigger & 8 != 0],
          cont: [cont & 1 != 0, cont & 2 != 0, cont & 4 != 0, cont & 8 != 0],
          op: op,
          args: args
        }
      })
      .collect()
  }
}