Run rustfmt on some files

This commit is contained in:
greg 2016-12-29 02:01:48 -08:00
parent d4d61ce5ad
commit af45004afa
2 changed files with 79 additions and 66 deletions

View File

@ -124,9 +124,7 @@ impl CodeGen for Expression {
let int_type = LLVMWrap::Int64TypeInContext(data.context); let int_type = LLVMWrap::Int64TypeInContext(data.context);
match self { match self {
&Variable(ref name) => { &Variable(ref name) => *data.variables.get(name).unwrap(),
*data.variables.get(name).unwrap()
},
&BinExp(ref op, ref left, ref right) if op == "=" => { &BinExp(ref op, ref left, ref right) if op == "=" => {
if let Variable(ref name) = **left { if let Variable(ref name) = **left {
let new_value = right.codegen(data); let new_value = right.codegen(data);
@ -135,7 +133,7 @@ impl CodeGen for Expression {
} else { } else {
panic!("Bad variable assignment") panic!("Bad variable assignment")
} }
}, }
&BinExp(ref op, ref left, ref right) => { &BinExp(ref op, ref left, ref right) => {
let lhs = left.codegen(data); let lhs = left.codegen(data);
let rhs = right.codegen(data); let rhs = right.codegen(data);
@ -149,12 +147,12 @@ impl CodeGen for Expression {
}; };
generator(data.builder, lhs, rhs, "temp") generator(data.builder, lhs, rhs, "temp")
}, }
&Number(ref n) => { &Number(ref n) => {
let native_val = *n as u64; let native_val = *n as u64;
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false); let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
int_value int_value
}, }
_ => unimplemented!(), _ => unimplemented!(),
} }
} }

View File

@ -10,7 +10,7 @@ enum SideEffect {
} }
struct Varmap { struct Varmap {
map: HashMap<String, Expression> map: HashMap<String, Expression>,
} }
impl Varmap { impl Varmap {
@ -37,18 +37,18 @@ pub struct Evaluator {
} }
impl Evaluator { impl Evaluator {
pub fn new() -> Evaluator { pub fn new() -> Evaluator {
Evaluator { varmap: Varmap::new(), Evaluator {
varmap: Varmap::new(),
funcmap: Funcmap::new(), funcmap: Funcmap::new(),
frames: Vec::new(), frames: Vec::new(),
} }
} }
pub fn run(&mut self, ast: AST) -> Vec<String> { pub fn run(&mut self, ast: AST) -> Vec<String> {
ast.into_iter().map(|astnode| { ast.into_iter()
self.reduce(astnode) .map(|astnode| self.reduce(astnode))
}).collect() .collect()
} }
fn add_binding(&mut self, var: String, value: Expression) { fn add_binding(&mut self, var: String, value: Expression) {
@ -109,7 +109,7 @@ impl Evaluator {
loop { loop {
node = self.step(node); node = self.step(node);
if !node.is_reducible() { if !node.is_reducible() {
break break;
} }
} }
@ -132,7 +132,7 @@ impl Evaluator {
for side_effect in l { for side_effect in l {
self.perform_side_effect(side_effect); self.perform_side_effect(side_effect);
} }
}, }
} }
} }
@ -146,12 +146,12 @@ impl Evaluator {
} else { } else {
(ExprNode(expr), None) (ExprNode(expr), None)
} }
}, }
FuncNode(func) => { FuncNode(func) => {
let fn_name = func.prototype.name.clone(); let fn_name = func.prototype.name.clone();
self.add_function(fn_name, func); self.add_function(fn_name, func);
(ExprNode(Expression::Null), None) (ExprNode(Expression::Null), None)
}, }
} }
} }
@ -166,7 +166,7 @@ impl Evaluator {
None => (Null, None), None => (Null, None),
Some(expr) => (expr, None), Some(expr) => (expr, None),
} }
}, }
BinExp(op, box left, box right) => { BinExp(op, box left, box right) => {
if right.is_reducible() { if right.is_reducible() {
let new = self.reduce_expr(right); let new = self.reduce_expr(right);
@ -179,8 +179,8 @@ impl Evaluator {
Variable(var) => { Variable(var) => {
self.add_binding(var, right); self.add_binding(var, right);
return (Null, None); //TODO variable binding should be an effect return (Null, None); //TODO variable binding should be an effect
}, }
_ => () _ => (),
} }
} }
@ -190,14 +190,12 @@ impl Evaluator {
} else { } else {
(self.reduce_binop(op, left, right), None) //can assume both arguments are maximally reduced (self.reduce_binop(op, left, right), None) //can assume both arguments are maximally reduced
} }
}, }
Call(name, mut args) => { Call(name, mut args) => {
let mut f = true; let mut f = true;
for arg in args.iter_mut() { for arg in args.iter_mut() {
if arg.is_reducible() { if arg.is_reducible() {
take_mut::take(arg, |arg| { take_mut::take(arg, |arg| self.reduce_expr(arg).0);
self.reduce_expr(arg).0
});
f = false; f = false;
break; break;
} }
@ -207,7 +205,7 @@ impl Evaluator {
} else { } else {
(Call(name, args), None) (Call(name, args), None)
} }
}, }
Conditional(_, _, _) => unimplemented!(), Conditional(_, _, _) => unimplemented!(),
} }
} }
@ -215,39 +213,56 @@ impl Evaluator {
fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression { fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression {
use parser::Expression::*; use parser::Expression::*;
match &op[..] { match &op[..] {
"+" => match (left, right) { "+" => {
match (left, right) {
(Number(l), Number(r)) => Number(l + r), (Number(l), Number(r)) => Number(l + r),
(StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)), (StringLiteral(s1), StringLiteral(s2)) => {
StringLiteral(format!("{}{}", s1, s2))
}
_ => Null, _ => Null,
}, }
"-" => match (left, right) { }
"-" => {
match (left, right) {
(Number(l), Number(r)) => Number(l - r), (Number(l), Number(r)) => Number(l - r),
_ => Null, _ => Null,
}, }
"*" => match (left, right) { }
"*" => {
match (left, right) {
(Number(l), Number(r)) => Number(l * r), (Number(l), Number(r)) => Number(l * r),
_ => Null, _ => Null,
}, }
"/" => match (left, right) { }
"/" => {
match (left, right) {
(Number(l), Number(r)) if r != 0.0 => Number(l / r), (Number(l), Number(r)) if r != 0.0 => Number(l / r),
_ => Null, _ => Null,
}, }
"%" => match (left, right) { }
"%" => {
match (left, right) {
(Number(l), Number(r)) => Number(l % r), (Number(l), Number(r)) => Number(l % r),
_ => Null, _ => Null,
}, }
"=" => match (left, right) { }
"=" => {
match (left, right) {
(Variable(var), right) => { (Variable(var), right) => {
self.add_binding(var, right); self.add_binding(var, right);
Null Null
}, }
_ => Null, _ => Null,
}, }
}
_ => Null, _ => Null,
} }
} }
fn reduce_call(&mut self, name: String, arguments: Vec<Expression>) -> (Expression, Option<SideEffect>) { fn reduce_call(&mut self,
name: String,
arguments: Vec<Expression>)
-> (Expression, Option<SideEffect>) {
use parser::Expression::*; use parser::Expression::*;
// ugly hack for now // ugly hack for now
@ -262,11 +277,11 @@ impl Evaluator {
let function = match self.lookup_function(name) { let function = match self.lookup_function(name) {
Some(func) => func, Some(func) => func,
None => return (Null, None) None => return (Null, None),
}; };
if function.prototype.parameters.len() != arguments.len() { if function.prototype.parameters.len() != arguments.len() {
return (Null, None) return (Null, None);
} }
let mut frame: Varmap = Varmap::new(); let mut frame: Varmap = Varmap::new();