add node helper method

This commit is contained in:
Masato Imai
2022-08-22 10:03:09 +09:00
parent f6342e17a4
commit 8f223df310
2 changed files with 204 additions and 33 deletions

View File

@ -88,6 +88,166 @@ pub enum Node {
None,
}
impl Node {
pub fn expect_function(
self,
) -> Result<
(
String,
Vec<String>,
Vec<GPSLType>,
Vec<Box<Node>>,
Option<Box<Node>>,
),
String,
> {
match self {
Node::Function {
name,
args_name,
args_type,
body,
attribute,
} => Ok((name, args_name, args_type, body, attribute)),
_ => Err("Expected function".to_string()),
}
}
pub fn expect_gpsltype(self) -> Result<GPSLType, String> {
match self {
Node::GPSLType { value } => Ok(value),
_ => Err("Expected GPSLType".to_string()),
}
}
pub fn expect_attribute(self) -> Result<(String, Vec<Box<Node>>), String> {
match self {
Node::Attribute { name, args } => Ok((name, args)),
_ => Err("Expected Attribute".to_string()),
}
}
pub fn expect_mode(self) -> Result<String, String> {
match self {
Node::Mode { mode } => Ok(mode),
_ => Err("Expected Mode".to_string()),
}
}
pub fn expect_permission(self) -> Result<(Vec<String>, Vec<String>), String> {
match self {
Node::Permission { accept, reject } => Ok((accept, reject)),
_ => Err("Expected Permission".to_string()),
}
}
pub fn expect_operator(self) -> Result<(NodeKind, Box<Node>, Box<Node>), String> {
match self {
Node::Operator { kind, lhs, rhs } => Ok((kind, lhs, rhs)),
_ => Err("Expected Operator".to_string()),
}
}
pub fn expect_number(self) -> Result<i64, String> {
match self {
Node::Number { value } => Ok(value),
_ => Err("Expected Number".to_string()),
}
}
pub fn expect_text(self) -> Result<String, String> {
match self {
Node::Text { value } => Ok(value),
_ => Err("Expected Text".to_string()),
}
}
pub fn expect_lvar(self) -> Result<(String, Option<Box<Node>>), String> {
match self {
Node::Lvar { value, index } => Ok((value, index)),
_ => Err("Expected LVar".to_string()),
}
}
pub fn expect_return(self) -> Result<Box<Node>, String> {
match self {
Node::Return { lhs } => Ok(lhs),
_ => Err("Expected Return".to_string()),
}
}
pub fn expect_if(self) -> Result<(Box<Node>, Box<Node>, Option<Box<Node>>), String> {
match self {
Node::If {
condition,
stmt,
else_stmt,
} => Ok((condition, stmt, else_stmt)),
_ => Err("Expected If".to_string()),
}
}
pub fn expect_while(self) -> Result<(Box<Node>, Box<Node>), String> {
match self {
Node::While { condition, stmt } => Ok((condition, stmt)),
_ => Err("Expected While".to_string()),
}
}
pub fn expect_for(
self,
) -> Result<
(
Option<Box<Node>>,
Option<Box<Node>>,
Option<Box<Node>>,
Box<Node>,
),
String,
> {
match self {
Node::For {
init,
condition,
update,
stmt,
} => Ok((init, condition, update, stmt)),
_ => Err("Expected For".to_string()),
}
}
pub fn expect_block(
self,
) -> Result<(Vec<Box<Node>>, Option<Box<Node>>, Option<Box<Node>>), String> {
match self {
Node::Block {
stmts,
permission,
mode,
} => Ok((stmts, permission, mode)),
_ => Err("Expected Block".to_string()),
}
}
pub fn expect_define(self) -> Result<(String, Option<GPSLType>, Option<Box<Node>>), String> {
match self {
Node::Define {
name,
var_type,
value,
} => Ok((name, var_type, value)),
_ => Err("Expected Define".to_string()),
}
}
pub fn expect_call(self) -> Result<(String, Vec<Box<Node>>), String> {
match self {
Node::Call { name, args } => Ok((name, args)),
_ => Err("Expected Call".to_string()),
}
}
}
impl Node {
pub fn new_node(kind: NodeKind, lhs: Box<Node>, rhs: Box<Node>) -> Box<Node> {
Box::new(Node::Operator { kind, lhs, rhs })

View File

@ -372,42 +372,53 @@ fn client(args: Args) {
let mut server_functions: HashMap<String, HashMap<String, Box<Node>>> = HashMap::new();
for function in functions.clone() {
match *function.clone().1 {
Node::Function { attribute, .. } => match *(attribute.unwrap()) {
Node::Attribute { name, args } => {
if name == String::from("server") {
let ip = {
let mut t_ip = None;
for arg in args {
let ip = match *arg {
Node::Operator { kind, lhs, rhs } => {
if kind == NodeKind::ASSIGN {
if lhs.extract_string() == String::from("ip") {
Some(rhs.extract_string())
} else {
None
}
} else {
None
}
}
_ => None,
};
if ip.is_some() {
t_ip = ip;
break;
}
}
t_ip.unwrap()
};
let function_node = function.clone();
let function = function.clone().1.expect_function();
if let Err(_) = function {
continue;
}
let function = function.unwrap();
if let None = function.4 {
continue;
}
let t_functions = server_functions.entry(ip).or_insert(HashMap::new());
t_functions.insert(function.clone().0.clone(), function.clone().1.clone());
let attribute = function.4.unwrap().expect_attribute();
if let Err(_) = attribute {
continue;
}
let attribute = attribute.unwrap();
let name = attribute.0;
let args = attribute.1;
if name == String::from("server") {
let ip = {
let mut t_ip = None;
for arg in args {
let ip = match *arg {
Node::Operator { kind, lhs, rhs } => {
if kind == NodeKind::ASSIGN {
if lhs.extract_string() == String::from("ip") {
Some(rhs.extract_string())
} else {
None
}
} else {
None
}
}
_ => None,
};
if ip.is_some() {
t_ip = ip;
break;
}
}
_ => {}
},
_ => {}
t_ip.unwrap()
};
let t_functions = server_functions.entry(ip).or_insert(HashMap::new());
t_functions.insert(function_node.clone().0.clone(), function_node.1);
}
}