optimize/src/visitor/constant_folder.rs

129 lines
3.8 KiB
Rust
Raw Normal View History

2021-05-18 17:40:26 +07:00
mod expression_reducer;
2021-05-19 17:26:21 +07:00
mod scope;
2021-05-18 17:40:26 +07:00
2021-05-17 21:15:40 +07:00
use full_moon::ast::*;
use crate::visitor::Visitor;
pub struct ConstantFolder {
2021-05-19 16:09:55 +07:00
locals: Box<Vec<expression_reducer::Variable>>
2021-05-17 21:15:40 +07:00
}
impl ConstantFolder {
2021-05-18 17:40:26 +07:00
pub fn new() -> Self {
ConstantFolder{
2021-05-19 16:09:55 +07:00
locals: Box::new(vec![])
2021-05-17 21:15:40 +07:00
}
}
2021-05-19 16:09:55 +07:00
fn register_local(&mut self, name: String, value: Option<Expression>) {
let locals = &*self.locals;
let var = expression_reducer::Variable::with_value(
name, value, locals
);
self.locals.push(var);
2021-05-18 17:40:26 +07:00
}
2021-05-19 16:09:55 +07:00
fn assign(&mut self, name: String, value: Option<Expression>) {
let locals = &*self.locals;
2021-05-18 17:40:26 +07:00
let var = expression_reducer::Variable::with_value(
2021-05-19 16:09:55 +07:00
name, value, locals
2021-05-18 17:40:26 +07:00
);
{
let mut iter = self.locals.iter_mut();
let it = iter.find(|it| it.name() == var.name());
it.map(|old| old.assign_value(var));
};
2021-05-17 21:15:40 +07:00
}
}
impl Visitor for ConstantFolder {
fn visit_assignment<'a>(&mut self, assignment: &Assignment<'a>) {
2021-05-18 17:40:26 +07:00
let mut names = assignment.variables().iter();
let mut expressions = assignment.expressions().iter();
loop {
let name = names.next().map(|x| match x {
2021-05-19 16:09:55 +07:00
Var::Expression(_var_expr) => {
panic!("assignment Var::Expression is not supported")
2021-05-18 17:40:26 +07:00
}
Var::Name(n) => n.token().to_string(),
2021-05-19 16:09:55 +07:00
_ => unreachable!()
2021-05-18 17:40:26 +07:00
});
let expr = expressions.next().map(|x| x.clone() /* reduced */);
2021-05-19 16:09:55 +07:00
match name {
Some(name) => {
self.assign(name, expr);
}
None => {
break;
}
2021-05-18 17:40:26 +07:00
}
}
2021-05-17 21:15:40 +07:00
}
fn visit_do<'a>(&mut self, do_: &Do<'a>) {
println!("visit do {}", do_);
}
fn visit_function_call<'a>(&mut self, function_call: &FunctionCall<'a>) {
println!("visit function_call {}", function_call);
2021-05-19 16:09:55 +07:00
let reduced = expression_reducer::reduce_expression_value_function_call(function_call.clone(), &*self.locals);
println!("{:?}", reduced);
2021-05-17 21:15:40 +07:00
}
fn visit_function_declaration<'a>(&mut self, function_declaration: &FunctionDeclaration<'a>) {
println!("visit function_declaration {}", function_declaration);
}
fn visit_generic_for<'a>(&mut self, generic_for: &GenericFor<'a>) {
println!("visit generic_for {}", generic_for);
}
fn visit_if<'a>(&mut self, if_: &If<'a>) {
println!("visit if {}", if_);
}
fn visit_local_assignment<'a>(&mut self, local_assignment: &LocalAssignment<'a>) {
2021-05-18 17:40:26 +07:00
let mut names = local_assignment.names().iter();
let mut expressions = local_assignment.expressions().iter();
loop {
let name = names.next().map(|x| x.token().to_string());
let expr = expressions.next().map(|x| x.clone() /* reduced */);
2021-05-19 16:09:55 +07:00
match name {
Some(name) => {
self.register_local(name, expr);
}
None => {
break;
}
2021-05-18 17:40:26 +07:00
}
}
2021-05-17 21:15:40 +07:00
}
fn visit_local_function<'a>(&mut self, local_function: &LocalFunction<'a>) {
println!("visit local_function {}", local_function);
}
fn visit_numeric_for<'a>(&mut self, numeric_for: &NumericFor<'a>) {
println!("visit numeric_for {}", numeric_for);
}
fn visit_repeat<'a>(&mut self, repeat: &Repeat<'a>) {
println!("visit repeat {}", repeat);
}
fn visit_while<'a>(&mut self, while_: &While<'a>) {
println!("visit while {}", while_);
}
fn visit_goto<'a>(&mut self, goto: &lua52::Goto<'a>) {
println!("visit goto {}", goto);
}
fn visit_label<'a>(&mut self, label: &lua52::Label<'a>) {
println!("visit label {}", label);
}
}