From 30115423762deef0ce0417d88abaafaf4b0a8abf Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Wed, 19 May 2021 17:26:50 +0700 Subject: [PATCH] format code --- src/main.rs | 8 +- src/visitor.rs | 4 +- src/visitor/constant_folder.rs | 23 +- .../constant_folder/expression_reducer.rs | 323 ++++++++---------- src/visitor/constant_folder/scope.rs | 11 +- src/visitor/visitor.rs | 44 +-- 6 files changed, 170 insertions(+), 243 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3d06e92..8cbadc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,16 @@ mod visitor; -use std::fs; use full_moon::parse; +use std::fs; -use visitor::Visitor; use visitor::ConstantFolder; +use visitor::Visitor; fn main() { // let contents = fs::read_to_string("main_android_org.lua") // let contents = fs::read_to_string("small.lua") - let contents = fs::read_to_string("very_small.lua") - .expect("Something went wrong reading the file"); + let contents = + fs::read_to_string("very_small.lua").expect("Something went wrong reading the file"); let tree = parse(&contents).expect("Parsing lua gone wrong"); diff --git a/src/visitor.rs b/src/visitor.rs index 08f5028..c7197bf 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -1,5 +1,5 @@ -mod visitor; mod constant_folder; +mod visitor; -pub use visitor::*; pub use constant_folder::*; +pub use visitor::*; diff --git a/src/visitor/constant_folder.rs b/src/visitor/constant_folder.rs index 6d55d3f..7223b2e 100644 --- a/src/visitor/constant_folder.rs +++ b/src/visitor/constant_folder.rs @@ -5,31 +5,26 @@ use full_moon::ast::*; use crate::visitor::Visitor; - pub struct ConstantFolder { - locals: Box> + locals: Box>, } impl ConstantFolder { pub fn new() -> Self { - ConstantFolder{ - locals: Box::new(vec![]) + ConstantFolder { + locals: Box::new(vec![]), } } fn register_local(&mut self, name: String, value: Option) { let locals = &*self.locals; - let var = expression_reducer::Variable::with_value( - name, value, locals - ); + let var = expression_reducer::Variable::with_value(name, value, locals); self.locals.push(var); } fn assign(&mut self, name: String, value: Option) { let locals = &*self.locals; - let var = expression_reducer::Variable::with_value( - name, value, locals - ); + let var = expression_reducer::Variable::with_value(name, value, locals); { let mut iter = self.locals.iter_mut(); let it = iter.find(|it| it.name() == var.name()); @@ -48,7 +43,7 @@ impl Visitor for ConstantFolder { panic!("assignment Var::Expression is not supported") } Var::Name(n) => n.token().to_string(), - _ => unreachable!() + _ => unreachable!(), }); let expr = expressions.next().map(|x| x.clone() /* reduced */); match name { @@ -68,7 +63,10 @@ impl Visitor for ConstantFolder { fn visit_function_call<'a>(&mut self, function_call: &FunctionCall<'a>) { println!("visit function_call {}", function_call); - let reduced = expression_reducer::reduce_expression_value_function_call(function_call.clone(), &*self.locals); + let reduced = expression_reducer::reduce_expression_value_function_call( + function_call.clone(), + &*self.locals, + ); println!("{:?}", reduced); } @@ -124,5 +122,4 @@ impl Visitor for ConstantFolder { fn visit_label<'a>(&mut self, label: &lua52::Label<'a>) { println!("visit label {}", label); } - } diff --git a/src/visitor/constant_folder/expression_reducer.rs b/src/visitor/constant_folder/expression_reducer.rs index 11ab9d9..7dcaca1 100644 --- a/src/visitor/constant_folder/expression_reducer.rs +++ b/src/visitor/constant_folder/expression_reducer.rs @@ -61,17 +61,14 @@ use full_moon::ast; #[derive(Debug)] pub struct Variable { n: String, - v: Expression + v: Expression, } impl Variable { pub fn with_value(name: String, expr: Option, locals: &Vec) -> Self { let v = Expression::reduce(expr, locals); println!("variable_with {} = {:?}", name, v); - Self { - n: name, - v: v, - } + Self { n: name, v: v } } pub fn name(&self) -> String { @@ -121,7 +118,7 @@ impl BinOp { ast::BinOp::TildeEqual(_) => BinOp::TildeEqual, ast::BinOp::TwoDots(_) => BinOp::TwoDots, ast::BinOp::TwoEqual(_) => BinOp::TwoEqual, - _ => unreachable!() + _ => unreachable!(), } } } @@ -139,7 +136,7 @@ impl UnOp { ast::UnOp::Minus(_) => UnOp::Minus, ast::UnOp::Not(_) => UnOp::Not, ast::UnOp::Hash(_) => UnOp::Hash, - _ => unreachable!() + _ => unreachable!(), } } } @@ -149,9 +146,9 @@ pub enum ExpressionType { Number(f64), Boolean(bool), String(String), - Var(String), // name - FunctionCall(String, Vec), // name, param - Module(String, Vec), // name, suffixes + Var(String), // name + FunctionCall(String, Vec), // name, param + Module(String, Vec), // name, suffixes BinOp(Box, Box, BinOp), // lhs, rhs, binop UnOp(Box, UnOp), Index(Box, Vec), // name, index expr @@ -166,7 +163,7 @@ pub struct Expression { impl Expression { pub fn reduce(expr: Option, locals: &Vec) -> Self { if expr.is_none() { - Expression{ + Expression { t: ExpressionType::Nil, } } else { @@ -199,34 +196,18 @@ impl Expression { fn reduce_expression(expr: ast::Expression, locals: &Vec) -> Expression { match expr { - ast::Expression::BinaryOperator { - lhs, - rhs, - binop - } => { + ast::Expression::BinaryOperator { lhs, rhs, binop } => { let lhs_ = reduce_expression(*lhs, locals); let rhs_ = reduce_expression(*rhs, locals); apply_binary_operator(lhs_, rhs_, binop) } - ast::Expression::Parentheses { - expression, - .. - } => { - reduce_expression(*expression, locals) - } - ast::Expression::UnaryOperator { - unop, - expression, - } => { + ast::Expression::Parentheses { expression, .. } => reduce_expression(*expression, locals), + ast::Expression::UnaryOperator { unop, expression } => { let expression_ = reduce_expression(*expression, locals); apply_unary_operator(expression_, unop) } - ast::Expression::Value { - value - } => { - reduce_expression_value(*value, locals) - } - _ => unreachable!() + ast::Expression::Value { value } => reduce_expression_value(*value, locals), + _ => unreachable!(), } } @@ -243,36 +224,32 @@ fn reduce_expression_value(value: ast::Value, locals: &Vec) -> Express } ast::Value::Number(number) => { let num = number.token().to_string(); - Expression{ + Expression { t: ExpressionType::Number(num.parse::().unwrap()), } } ast::Value::ParenthesesExpression(parentheses_expression) => { reduce_expression(parentheses_expression, locals) } - ast::Value::String(string) => { - Expression{ - t: ExpressionType::String(string.token().to_string()), - } - } + ast::Value::String(string) => Expression { + t: ExpressionType::String(string.token().to_string()), + }, ast::Value::Symbol(symbol) => { let s = symbol.token().to_string(); if s == "true" { - Expression{ + Expression { t: ExpressionType::Boolean(true), } } else if s == "false" { - Expression{ + Expression { t: ExpressionType::Boolean(false), } } else { panic!("reduce Expression::Symbol is not true/false"); } } - ast::Value::Var(var) => { - reduce_expression_value_var(var, locals) - } - _ => unreachable!() + ast::Value::Var(var) => reduce_expression_value_var(var, locals), + _ => unreachable!(), } } @@ -282,196 +259,176 @@ fn reduce_expression_value_var(var: ast::Var, locals: &Vec) -> Express let prefix = { match expr.prefix() { ast::Prefix::Name(name) => name.token().to_string(), - _ => panic!("Var::Expression prefix expression is unsupported") + _ => panic!("Var::Expression prefix expression is unsupported"), } }; - let suffixes = expr.suffixes().map(|suf| { - match suf { - ast::Suffix::Index(ast::Index::Dot{ - name, - .. - }) => { - name.token().to_string() - } - _ => panic!("Var::Expression Call not supported") - } - }).collect::>(); + let suffixes = expr + .suffixes() + .map(|suf| match suf { + ast::Suffix::Index(ast::Index::Dot { name, .. }) => name.token().to_string(), + _ => panic!("Var::Expression Call not supported"), + }) + .collect::>(); let local_var = locals.iter().find(|it| it.name() == prefix); match local_var { - None => { - Expression { - t: ExpressionType::Module(prefix, suffixes) - } - } - Some(var) => { - match var.v.clone().t { - ExpressionType::Module(n, _) => { - Expression { - t: ExpressionType::Module(n, suffixes) - } - } - _ => { - Expression { - t: ExpressionType::Module(var.name(), suffixes) - } - } - } - } + None => Expression { + t: ExpressionType::Module(prefix, suffixes), + }, + Some(var) => match var.v.clone().t { + ExpressionType::Module(n, _) => Expression { + t: ExpressionType::Module(n, suffixes), + }, + _ => Expression { + t: ExpressionType::Module(var.name(), suffixes), + }, + }, } } ast::Var::Name(name) => { fn is_standard_library(name: &String) -> bool { - let standard_library_names: Vec<&str> = vec![ - "math", "string", "table", "bit" - ]; + let standard_library_names: Vec<&str> = vec!["math", "string", "table", "bit"]; standard_library_names.contains(&name.as_str()) } let name_str = name.token().to_string(); if is_standard_library(&name_str) { return Expression { - t: ExpressionType::Module(name_str, vec![]) + t: ExpressionType::Module(name_str, vec![]), }; } let local_var = locals.iter().find(|it| it.name() == name_str); match local_var { - None => { - Expression { - t: ExpressionType::Var(name_str) - } - } - Some(v) => { - v.v.clone() - } + None => Expression { + t: ExpressionType::Var(name_str), + }, + Some(v) => v.v.clone(), } } - _ => unreachable!() + _ => unreachable!(), } } -pub fn reduce_expression_value_function_call(function_call: ast::FunctionCall, locals: &Vec) -> Expression { +pub fn reduce_expression_value_function_call( + function_call: ast::FunctionCall, + locals: &Vec, +) -> Expression { let function_name = match function_call.prefix() { ast::Prefix::Name(name) => name.token().to_string(), - _ => panic!("Function name as expression is unsupported") + _ => panic!("Function name as expression is unsupported"), }; let args = { let suf = function_call.suffixes().next().unwrap(); match suf { - ast::Suffix::Call(ast::Call::AnonymousCall(ast::FunctionArgs::Parentheses{ + ast::Suffix::Call(ast::Call::AnonymousCall(ast::FunctionArgs::Parentheses { arguments, .. - })) => { - arguments.iter().map(|x| reduce_expression(x.clone(), locals)).collect::>() - } - _ => panic!("Index not supported") + })) => arguments + .iter() + .map(|x| reduce_expression(x.clone(), locals)) + .collect::>(), + _ => panic!("Index not supported"), } }; Expression { - t: ExpressionType::FunctionCall(function_name, args) + t: ExpressionType::FunctionCall(function_name, args), } } fn apply_binary_operator(lhs: Expression, rhs: Expression, binop: ast::BinOp) -> Expression { match (&lhs.t, &rhs.t) { - (&ExpressionType::Number(lhs_), &ExpressionType::Number(rhs_)) => { - match binop { - ast::BinOp::GreaterThan(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ > rhs_), - } - }, - ast::BinOp::GreaterThanEqual(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ >= rhs_), - } + (&ExpressionType::Number(lhs_), &ExpressionType::Number(rhs_)) => match binop { + ast::BinOp::GreaterThan(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ > rhs_), } - ast::BinOp::LessThan(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ < rhs_), - } - } - ast::BinOp::LessThanEqual(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ <= rhs_), - } - } - ast::BinOp::Minus(_) => { - return Expression { - t: ExpressionType::Number(lhs_ - rhs_), - } - } - ast::BinOp::Percent(_) => { - return Expression { - t: ExpressionType::Number(lhs_ % rhs_), - } - } - ast::BinOp::Plus(_) => { - return Expression { - t: ExpressionType::Number(lhs_ + rhs_), - } - } - ast::BinOp::Slash(_) => { - return Expression { - t: ExpressionType::Number(lhs_ / rhs_), - } - } - ast::BinOp::Star(_) => { - return Expression { - t: ExpressionType::Number(lhs_ * rhs_), - } - } - ast::BinOp::Caret(_) => { - return Expression { - t: ExpressionType::Number(lhs_.powf(rhs_)), - } - } - ast::BinOp::TwoEqual(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ == rhs_), - } - } - ast::BinOp::TildeEqual(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ != rhs_), - } - } - _ => {} } - } + ast::BinOp::GreaterThanEqual(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ >= rhs_), + } + } + ast::BinOp::LessThan(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ < rhs_), + } + } + ast::BinOp::LessThanEqual(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ <= rhs_), + } + } + ast::BinOp::Minus(_) => { + return Expression { + t: ExpressionType::Number(lhs_ - rhs_), + } + } + ast::BinOp::Percent(_) => { + return Expression { + t: ExpressionType::Number(lhs_ % rhs_), + } + } + ast::BinOp::Plus(_) => { + return Expression { + t: ExpressionType::Number(lhs_ + rhs_), + } + } + ast::BinOp::Slash(_) => { + return Expression { + t: ExpressionType::Number(lhs_ / rhs_), + } + } + ast::BinOp::Star(_) => { + return Expression { + t: ExpressionType::Number(lhs_ * rhs_), + } + } + ast::BinOp::Caret(_) => { + return Expression { + t: ExpressionType::Number(lhs_.powf(rhs_)), + } + } + ast::BinOp::TwoEqual(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ == rhs_), + } + } + ast::BinOp::TildeEqual(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ != rhs_), + } + } + _ => {} + }, _ => {} }; match (&lhs.t, &rhs.t) { - (&ExpressionType::Boolean(lhs_), &ExpressionType::Boolean(rhs_)) => { - match binop { - ast::BinOp::And(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ & rhs_), - } + (&ExpressionType::Boolean(lhs_), &ExpressionType::Boolean(rhs_)) => match binop { + ast::BinOp::And(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ & rhs_), } - ast::BinOp::Or(_) => { - return Expression { - t: ExpressionType::Boolean(lhs_ | rhs_), - } - } - _ => {} } - } + ast::BinOp::Or(_) => { + return Expression { + t: ExpressionType::Boolean(lhs_ | rhs_), + } + } + _ => {} + }, _ => {} } match (&lhs.t, &rhs.t) { - (&ExpressionType::String(ref lhs_), &ExpressionType::String(ref rhs_)) => { - match binop { - ast::BinOp::TwoDots(_) => { - return Expression { - t: ExpressionType::String(lhs_.clone() + &rhs_), - } + (&ExpressionType::String(ref lhs_), &ExpressionType::String(ref rhs_)) => match binop { + ast::BinOp::TwoDots(_) => { + return Expression { + t: ExpressionType::String(lhs_.clone() + &rhs_), } - _ => {} } - } + _ => {} + }, _ => {} } @@ -486,20 +443,20 @@ fn apply_unary_operator(expr: Expression, unop: ast::UnOp) -> Expression { if let ExpressionType::Boolean(e) = expr.t { return Expression { t: ExpressionType::Boolean(!e), - } + }; } } ast::UnOp::Minus(_) => { if let ExpressionType::Number(e) = expr.t { return Expression { t: ExpressionType::Number(-e), - } + }; } } ast::UnOp::Hash(_) => { panic!("Unary Hash unimplemented") } - _ => unreachable!() + _ => unreachable!(), } Expression { diff --git a/src/visitor/constant_folder/scope.rs b/src/visitor/constant_folder/scope.rs index 5ad95b7..a70285f 100644 --- a/src/visitor/constant_folder/scope.rs +++ b/src/visitor/constant_folder/scope.rs @@ -8,9 +8,7 @@ pub struct Scope { impl Scope { pub fn new() -> Self { - Self { - scope: vec![], - } + Self { scope: vec![] } } fn frame(&self) -> Option<&Local> { @@ -30,11 +28,8 @@ impl Scope { } pub fn find_name(&self, name: String) -> Option<&Variable> { - self.frame().map_or(None, |frame| { - frame - .iter() - .find(|var| var.name() == name) - }) + self.frame() + .map_or(None, |frame| frame.iter().find(|var| var.name() == name)) } pub fn set_var(&mut self, value: Variable) { diff --git a/src/visitor/visitor.rs b/src/visitor/visitor.rs index a62893c..f15e236 100644 --- a/src/visitor/visitor.rs +++ b/src/visitor/visitor.rs @@ -10,39 +10,21 @@ pub trait Visitor { fn visit_statement<'a>(&mut self, stmt: &Stmt<'a>) { match stmt { - Stmt::Assignment(assignment) => { - self.visit_assignment(assignment) - } - Stmt::Do(do_) => { - self.visit_do(do_) - } - Stmt::FunctionCall(function_call) => { - self.visit_function_call(function_call) - } + Stmt::Assignment(assignment) => self.visit_assignment(assignment), + Stmt::Do(do_) => self.visit_do(do_), + Stmt::FunctionCall(function_call) => self.visit_function_call(function_call), Stmt::FunctionDeclaration(function_declaration) => { self.visit_function_declaration(function_declaration) } - Stmt::GenericFor(generic_for) => { - self.visit_generic_for(generic_for) - } - Stmt::If(if_) => { - self.visit_if(if_) - } + Stmt::GenericFor(generic_for) => self.visit_generic_for(generic_for), + Stmt::If(if_) => self.visit_if(if_), Stmt::LocalAssignment(local_assignment) => { self.visit_local_assignment(local_assignment) } - Stmt::LocalFunction(local_function) => { - self.visit_local_function(local_function) - } - Stmt::NumericFor(numeric_for) => { - self.visit_numeric_for(numeric_for) - } - Stmt::Repeat(repeat) => { - self.visit_repeat(repeat) - } - Stmt::While(while_) => { - self.visit_while(while_) - } + Stmt::LocalFunction(local_function) => self.visit_local_function(local_function), + Stmt::NumericFor(numeric_for) => self.visit_numeric_for(numeric_for), + Stmt::Repeat(repeat) => self.visit_repeat(repeat), + Stmt::While(while_) => self.visit_while(while_), // Stmt::CompoundAssignment(compound_assignment) => { // self.visit_compound_assignment(compound_assignment) // } @@ -52,12 +34,8 @@ pub trait Visitor { // Stmt::TypeDeclaration(type_declaration) => { // self.visit_type_declaration(type_declaration) // } - Stmt::Goto(goto) => { - self.visit_goto(goto) - } - Stmt::Label(label) => { - self.visit_label(label) - } + Stmt::Goto(goto) => self.visit_goto(goto), + Stmt::Label(label) => self.visit_label(label), _ => {} }; }