|
|
|
@ -2,7 +2,7 @@ use debug_cell::RefCell;
|
|
|
|
|
use std::{collections::HashMap, rc::Rc};
|
|
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
|
command::{parse_command, Command},
|
|
|
|
|
command::{parse_command, Command, AssignType},
|
|
|
|
|
control::{parse_control, Control},
|
|
|
|
|
event::Event,
|
|
|
|
|
token::Token,
|
|
|
|
@ -25,41 +25,50 @@ pub struct CommandContext {
|
|
|
|
|
impl CommandContext {
|
|
|
|
|
pub fn execute(&self) -> Option<Event> {
|
|
|
|
|
use Command::*;
|
|
|
|
|
use AssignType::*;
|
|
|
|
|
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()
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.insert(variable.clone(), value.clone());
|
|
|
|
|
return None;
|
|
|
|
|
},
|
|
|
|
|
Let { variable, value } => {
|
|
|
|
|
if let Token::Keyword(_) = value {
|
|
|
|
|
todo!("assignment variable interpolation isn't implemented");
|
|
|
|
|
}
|
|
|
|
|
self.context
|
|
|
|
|
.borrow()
|
|
|
|
|
.variables
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.insert(variable.clone(), value.clone());
|
|
|
|
|
return None;
|
|
|
|
|
Command::Assign { assign_type, variable, value } => match assign_type {
|
|
|
|
|
Define => panic!("define command should not be executed at runtime!"),
|
|
|
|
|
GlobalAssign => {
|
|
|
|
|
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()
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.insert(variable.clone(), value.clone());
|
|
|
|
|
return None;
|
|
|
|
|
},
|
|
|
|
|
Let => {
|
|
|
|
|
if let Token::Keyword(_) = value {
|
|
|
|
|
todo!("assignment variable interpolation isn't implemented");
|
|
|
|
|
}
|
|
|
|
|
self.context
|
|
|
|
|
.borrow()
|
|
|
|
|
.variables
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.insert(variable.clone(), value.clone());
|
|
|
|
|
return None;
|
|
|
|
|
},
|
|
|
|
|
Assign => {
|
|
|
|
|
if let Token::Keyword(_) = value {
|
|
|
|
|
todo!("assignment variable interpolation isn't implemented");
|
|
|
|
|
}
|
|
|
|
|
todo!("local variable assignment isn't implemented")
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
Eat { .. } => return None,
|
|
|
|
|
}
|
|
|
|
@ -178,7 +187,7 @@ pub fn parse_block(
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Rule::Line => BlockElement::Command(match parse_command(pair) {
|
|
|
|
|
Command::Define { variable, value } => {
|
|
|
|
|
Command::Assign { variable, value, assign_type: AssignType::Define } => {
|
|
|
|
|
let mut value = value;
|
|
|
|
|
if let Token::Keyword(keyword) = value {
|
|
|
|
|
value = definitions
|
|
|
|
|