Skip to content

Commit

Permalink
style: Run rustfmt on vm
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Aug 11, 2017
1 parent ee199da commit cc27f0d
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 102 deletions.
2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ lalrpop = { version = "0.13.1", optional = true }
[dev-dependencies]
# HACK Trick crates.io into letting letting this be published with a dependency on gluon
# (which requires gluon_vm to be published)
# gluon = { path = "..", version = "<0.6.0, >=0.4.2" } # GLUON
gluon = { path = "..", version = "<0.6.0, >=0.4.2" } # GLUON

lalrpop-util = "0.13.1"
regex = "0.2.0"
Expand Down
78 changes: 42 additions & 36 deletions vm/src/core/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::ops::{Deref, DerefMut};
use base::ast::{TypedIdent, Typed};
use base::ast::{Typed, TypedIdent};
use base::fnv::FnvSet;
use base::resolve;
use base::kind::{ArcKind, KindEnv};
use base::types::{self, Alias, ArcType, RecordSelector, Type, TypeEnv};
use base::scoped_map::ScopedMap;
use base::symbol::{Symbol, SymbolRef};
use core::{self, Allocator, CExpr, Expr, Pattern};
use core::optimize::{ExprProducer, Visitor, DifferentLifetime, SameLifetime, walk_expr_alloc};
use core::optimize::{walk_expr_alloc, DifferentLifetime, ExprProducer, SameLifetime, Visitor};
use types::*;
use self::Variable::*;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl<'l, 'g> ReducedExpr<'l, 'g> {
}
}

impl <'l, 'g> From<CExpr<'l>> for ReducedExpr<'l, 'g> {
impl<'l, 'g> From<CExpr<'l>> for ReducedExpr<'l, 'g> {
fn from(expr: CExpr<'l>) -> Self {
ReducedExpr::Local(expr)
}
Expand Down Expand Up @@ -225,7 +225,10 @@ impl<'a, 'e> TypeEnv for Compiler<'a, 'e> {
}

impl<'a, 'e> Compiler<'a, 'e> {
pub fn new(allocator: &'e Allocator<'e>, globals: &'a Fn(&Symbol) -> Option<CExpr<'a>>) -> Compiler<'a, 'e> {
pub fn new(
allocator: &'e Allocator<'e>,
globals: &'a Fn(&Symbol) -> Option<CExpr<'a>>,
) -> Compiler<'a, 'e> {
Compiler {
allocator,
globals,
Expand All @@ -234,15 +237,17 @@ impl<'a, 'e> Compiler<'a, 'e> {
}
}

fn find(&self, id: &Symbol, current: &mut FunctionEnvs<'e, 'a>) -> Option<Variable<'e, 'a, CExpr<'a>>> {
fn find(
&self,
id: &Symbol,
current: &mut FunctionEnvs<'e, 'a>,
) -> Option<Variable<'e, 'a, CExpr<'a>>> {
let variable = self.stack_constructors
.iter()
.filter_map(|(_, typ)| match **typ {
Type::Variant(ref row) => {
row.row_iter()
.enumerate()
.find(|&(_, field)| field.name == *id)
}
Type::Variant(ref row) => row.row_iter()
.enumerate()
.find(|&(_, field)| field.name == *id),
_ => None,
})
.next()
Expand Down Expand Up @@ -279,8 +284,11 @@ impl<'a, 'e> Compiler<'a, 'e> {
debug!("Interpreting: {}", expr);
let new_expr = self.compile(expr, &mut env)?;
env.end_function(self);
Ok(new_expr.map(|expr| expr.into_local(self.allocator))
.unwrap_or(expr))
Ok(
new_expr
.map(|expr| expr.into_local(self.allocator))
.unwrap_or(expr),
)
}

fn load_identifier(
Expand All @@ -289,22 +297,20 @@ impl<'a, 'e> Compiler<'a, 'e> {
function: &mut FunctionEnvs<'e, 'a>,
) -> Result<Option<ReducedExpr<'e, 'a>>> {
match self.find(id, function) {
Some(variable) => {
match variable {
Stack(expr) => {
if let Some(e) = expr {
debug!("Loading `{}` as `{}`", id, e.as_ref());
} else {
debug!("Unable to fold `{}`", id);
}
Ok(expr)
Some(variable) => match variable {
Stack(expr) => {
if let Some(e) = expr {
debug!("Loading `{}` as `{}`", id, e.as_ref());
} else {
debug!("Unable to fold `{}`", id);
}
Global(expr) => Ok(Some(ReducedExpr::Global(expr))),
Constructor(..) => Ok(None),
Ok(expr)
}
}
Global(expr) => Ok(Some(ReducedExpr::Global(expr))),
Constructor(..) => Ok(None),
},
// Can't inline what we can't resolve
None => Ok(None)
None => Ok(None),
}
}

Expand Down Expand Up @@ -401,20 +407,17 @@ impl<'a, 'e> Compiler<'a, 'e> {
}
return Ok(TailCall::Tail(body));
}
Expr::Call(..) => {
self.walk_expr(expr, function)?
}
Expr::Call(..) => self.walk_expr(expr, function)?,
Expr::Match(expr, alts) => {
let expr = self.compile(expr, function)?.unwrap_or(ReducedExpr::Local(expr));
let expr = self.compile(expr, function)?
.unwrap_or(ReducedExpr::Local(expr));
for alt in alts {
self.stack_constructors.enter_scope();
function.stack.enter_scope();
match alt.pattern {
Pattern::Constructor(_, ref args) => {
for arg in args.iter() {
function.push_unknown_stack_var(arg.name.clone());
}
}
Pattern::Constructor(_, ref args) => for arg in args.iter() {
function.push_unknown_stack_var(arg.name.clone());
},
Pattern::Record { .. } => {
let typ = &expr.as_ref().env_type_of(self);
self.compile_let_pattern(&alt.pattern, expr, typ, function)?;
Expand All @@ -423,7 +426,8 @@ impl<'a, 'e> Compiler<'a, 'e> {
function.push_stack_var(id.name.clone(), expr);
}
}
let new_expr = self.compile(&alt.expr, function)?.unwrap_or(ReducedExpr::Local(&alt.expr));
let new_expr = self.compile(&alt.expr, function)?
.unwrap_or(ReducedExpr::Local(&alt.expr));
function.exit_scope();
self.stack_constructors.exit_scope();
match alt.pattern {
Expand Down Expand Up @@ -581,7 +585,9 @@ mod tests {
let _ = ::env_logger::init();

let global_alloc = Allocator::new();
let global: CExpr = global_alloc.arena.alloc(Expr::Const(Literal::Int(1), Span::default()));
let global: CExpr = global_alloc
.arena
.alloc(Expr::Const(Literal::Int(1), Span::default()));

let expr = r#"
x"#;
Expand Down
12 changes: 6 additions & 6 deletions vm/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,7 @@ mod internal {

impl CoreExpr {
pub fn new(allocator: Allocator<'static>, expr: Expr<'static>) -> CoreExpr {
CoreExpr {
allocator,
expr,
}
CoreExpr { allocator, expr }
}

// unsafe: The lifetimes of the returned `Expr` must be bound to `&self`
Expand Down Expand Up @@ -380,7 +377,10 @@ pub fn translate(env: &PrimitiveEnv, expr: &SpannedExpr<Symbol>) -> CoreExpr {
let core_expr = (*(&translator as *const Translator)).translate(expr);
transmute::<Expr, Expr<'static>>(core_expr)
};
CoreExpr::new(transmute::<Allocator, Allocator<'static>>(translator.allocator), core_expr)
CoreExpr::new(
transmute::<Allocator, Allocator<'static>>(translator.allocator),
core_expr,
)
}
}

Expand Down Expand Up @@ -822,7 +822,7 @@ struct ReplaceVariables<'a> {
replacements: HashMap<Symbol, Symbol>,
allocator: &'a Allocator<'a>,
}

impl<'a> Visitor<'a, 'a> for ReplaceVariables<'a> {
type Producer = SameLifetime<'a>;

Expand Down

0 comments on commit cc27f0d

Please sign in to comment.