|
|
@ -1,23 +1,67 @@
|
|
|
|
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
|
|
|
use std::{collections::HashMap, rc::Rc};
|
|
|
|
|
|
|
|
use debug_cell::RefCell;
|
|
|
|
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
use super::{
|
|
|
|
command::{parse_command, Command},
|
|
|
|
command::{parse_command, Command},
|
|
|
|
control::{parse_control, Control},
|
|
|
|
control::{parse_control, Control},
|
|
|
|
token::Token,
|
|
|
|
token::Token,
|
|
|
|
Pair, Rule,
|
|
|
|
Pair, Rule, event::Event,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
|
|
pub struct CommandBlock {
|
|
|
|
pub struct CommandBlock {
|
|
|
|
parent: Option<Rc<RefCell<CommandBlock>>>,
|
|
|
|
parent: Option<Rc<RefCell<CommandBlock>>>,
|
|
|
|
control: Option<Control>,
|
|
|
|
control: Option<Control>,
|
|
|
|
elements: Vec<BlockElement>,
|
|
|
|
elements: Vec<BlockElement>,
|
|
|
|
next: Option<usize>,
|
|
|
|
next: Option<usize>,
|
|
|
|
variables: Option<Rc<RefCell<HashMap<String, Token>>>>,
|
|
|
|
pub variables: Option<Rc<RefCell<HashMap<String, Token>>>>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct CommandContext {
|
|
|
|
|
|
|
|
pub context: Rc<RefCell<CommandBlock>>,
|
|
|
|
|
|
|
|
pub command: Command,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl CommandContext {
|
|
|
|
|
|
|
|
pub fn execute(&self) -> Option<Event> {
|
|
|
|
|
|
|
|
use Command::*;
|
|
|
|
|
|
|
|
Some(
|
|
|
|
|
|
|
|
match &self.command {
|
|
|
|
|
|
|
|
Say { name, text } => Event::Say {
|
|
|
|
|
|
|
|
name: name.clone(),
|
|
|
|
|
|
|
|
text: text.clone(),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Define { .. } => panic!("define command should not be executed at runtime!"),
|
|
|
|
|
|
|
|
Assign { variable, value } => {
|
|
|
|
|
|
|
|
if let Token::Keyword(_) = value {
|
|
|
|
|
|
|
|
todo!("assignment variable interpolation isn't implemented");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let root = self.context
|
|
|
|
|
|
|
|
.borrow()
|
|
|
|
|
|
|
|
.get_root(&self.context);
|
|
|
|
|
|
|
|
// Don't want to re-borrow if root is self
|
|
|
|
|
|
|
|
let root = if Rc::ptr_eq(&self.context, &root) {
|
|
|
|
|
|
|
|
&self.context
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
&root
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
root
|
|
|
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
|
|
|
.variables
|
|
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
|
|
|
.insert(variable.clone(), value.clone());
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Eat { .. } => return None,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.process(&self.context),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl CommandBlock {
|
|
|
|
impl CommandBlock {
|
|
|
|
pub fn next(&mut self) -> Option<Command> {
|
|
|
|
pub fn next<'a>(&mut self, self_rc: &Rc<RefCell<Self>>) -> Option<CommandContext> {
|
|
|
|
let mut next = match self.next {
|
|
|
|
let mut next = match self.next {
|
|
|
|
Some(next) => next,
|
|
|
|
Some(next) => next,
|
|
|
|
None => return None,
|
|
|
|
None => return None,
|
|
|
@ -30,13 +74,16 @@ impl CommandBlock {
|
|
|
|
for element in &mut self.elements[next..] {
|
|
|
|
for element in &mut self.elements[next..] {
|
|
|
|
match element {
|
|
|
|
match element {
|
|
|
|
BlockElement::Command(command) => {
|
|
|
|
BlockElement::Command(command) => {
|
|
|
|
result = Some(command.clone());
|
|
|
|
result = Some(CommandContext {
|
|
|
|
|
|
|
|
context: self_rc.clone(),
|
|
|
|
|
|
|
|
command: command.clone(),
|
|
|
|
|
|
|
|
});
|
|
|
|
next += 1;
|
|
|
|
next += 1;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BlockElement::Block(block) => match block.borrow_mut().next() {
|
|
|
|
BlockElement::Block(block) => match block.borrow_mut().next(&block) {
|
|
|
|
Some(command) => {
|
|
|
|
Some(context_command) => {
|
|
|
|
result = Some(command.clone());
|
|
|
|
result = Some(context_command);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
None => {
|
|
|
@ -71,6 +118,14 @@ impl CommandBlock {
|
|
|
|
None => HashMap::new(),
|
|
|
|
None => HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_root(&self, self_rc: &Rc<RefCell<Self>>) -> Rc<RefCell<Self>> {
|
|
|
|
|
|
|
|
if let Some(parent) = &self.parent {
|
|
|
|
|
|
|
|
parent.borrow().get_root(&parent)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
self_rc.clone()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for CommandBlock {
|
|
|
|
impl Default for CommandBlock {
|
|
|
@ -85,7 +140,6 @@ impl Default for CommandBlock {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
|
|
enum BlockElement {
|
|
|
|
enum BlockElement {
|
|
|
|
Command(Command),
|
|
|
|
Command(Command),
|
|
|
|
Block(Rc<RefCell<CommandBlock>>),
|
|
|
|
Block(Rc<RefCell<CommandBlock>>),
|
|
|
|