format code
This commit is contained in:
parent
236c3aeb68
commit
3011542376
@ -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");
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
mod visitor;
|
||||
mod constant_folder;
|
||||
mod visitor;
|
||||
|
||||
pub use visitor::*;
|
||||
pub use constant_folder::*;
|
||||
pub use visitor::*;
|
||||
|
@ -5,31 +5,26 @@ use full_moon::ast::*;
|
||||
|
||||
use crate::visitor::Visitor;
|
||||
|
||||
|
||||
pub struct ConstantFolder {
|
||||
locals: Box<Vec<expression_reducer::Variable>>
|
||||
locals: Box<Vec<expression_reducer::Variable>>,
|
||||
}
|
||||
|
||||
impl ConstantFolder {
|
||||
pub fn new() -> Self {
|
||||
ConstantFolder {
|
||||
locals: Box::new(vec![])
|
||||
locals: Box::new(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
fn register_local(&mut self, name: String, value: Option<Expression>) {
|
||||
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<Expression>) {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<ast::Expression>, locals: &Vec<Variable>) -> 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!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -199,34 +196,18 @@ impl Expression {
|
||||
|
||||
fn reduce_expression(expr: ast::Expression, locals: &Vec<Variable>) -> 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!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -250,11 +231,9 @@ fn reduce_expression_value(value: ast::Value, locals: &Vec<Variable>) -> Express
|
||||
ast::Value::ParenthesesExpression(parentheses_expression) => {
|
||||
reduce_expression(parentheses_expression, locals)
|
||||
}
|
||||
ast::Value::String(string) => {
|
||||
Expression{
|
||||
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" {
|
||||
@ -269,10 +248,8 @@ fn reduce_expression_value(value: ast::Value, locals: &Vec<Variable>) -> Express
|
||||
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,78 +259,63 @@ fn reduce_expression_value_var(var: ast::Var, locals: &Vec<Variable>) -> 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::<Vec<String>>();
|
||||
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::<Vec<String>>();
|
||||
|
||||
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)
|
||||
None => Expression {
|
||||
t: ExpressionType::Var(name_str),
|
||||
},
|
||||
Some(v) => v.v.clone(),
|
||||
}
|
||||
}
|
||||
Some(v) => {
|
||||
v.v.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => unreachable!()
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reduce_expression_value_function_call(function_call: ast::FunctionCall, locals: &Vec<Variable>) -> Expression {
|
||||
pub fn reduce_expression_value_function_call(
|
||||
function_call: ast::FunctionCall,
|
||||
locals: &Vec<Variable>,
|
||||
) -> 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();
|
||||
@ -361,26 +323,26 @@ pub fn reduce_expression_value_function_call(function_call: ast::FunctionCall, l
|
||||
ast::Suffix::Call(ast::Call::AnonymousCall(ast::FunctionArgs::Parentheses {
|
||||
arguments,
|
||||
..
|
||||
})) => {
|
||||
arguments.iter().map(|x| reduce_expression(x.clone(), locals)).collect::<Vec<_>>()
|
||||
}
|
||||
_ => panic!("Index not supported")
|
||||
})) => arguments
|
||||
.iter()
|
||||
.map(|x| reduce_expression(x.clone(), locals))
|
||||
.collect::<Vec<_>>(),
|
||||
_ => 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 {
|
||||
(&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_),
|
||||
@ -437,14 +399,12 @@ fn apply_binary_operator(lhs: Expression, rhs: Expression, binop: ast::BinOp) ->
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
|
||||
match (&lhs.t, &rhs.t) {
|
||||
(&ExpressionType::Boolean(lhs_), &ExpressionType::Boolean(rhs_)) => {
|
||||
match binop {
|
||||
(&ExpressionType::Boolean(lhs_), &ExpressionType::Boolean(rhs_)) => match binop {
|
||||
ast::BinOp::And(_) => {
|
||||
return Expression {
|
||||
t: ExpressionType::Boolean(lhs_ & rhs_),
|
||||
@ -456,22 +416,19 @@ fn apply_binary_operator(lhs: Expression, rhs: Expression, binop: ast::BinOp) ->
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match (&lhs.t, &rhs.t) {
|
||||
(&ExpressionType::String(ref lhs_), &ExpressionType::String(ref rhs_)) => {
|
||||
match binop {
|
||||
(&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 {
|
||||
|
@ -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) {
|
||||
|
@ -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),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user