Run rustfmt

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-10-20 16:21:50 -07:00
parent ceda48988d
commit 692bb521ec
27 changed files with 402 additions and 251 deletions

View File

@@ -106,8 +106,7 @@ impl Thunk {
}
pub fn flatten(self) -> BasicBlockList {
Flatten::default()
.flatten(self)
Flatten::default().flatten(self)
}
}
@@ -158,10 +157,13 @@ impl Flatten {
match thunk {
Thunk::Body(thunk) => {
let this_block = self.this_block();
let prev = self.blocks.insert(this_block, BasicBlock::Block {
exit: next_block,
block: thunk,
});
let prev = self.blocks.insert(
this_block,
BasicBlock::Block {
exit: next_block,
block: thunk,
},
);
assert!(prev.is_none());
}
Thunk::List(thunks) => {
@@ -172,14 +174,20 @@ impl Flatten {
}
assert_eq!(next_block, self.this_block());
}
Thunk::Branch { thunk_true, thunk_false, } => {
Thunk::Branch {
thunk_true,
thunk_false,
} => {
let branch_block = self.this_block();
let block_true = self.this_block() + 1;
let block_false = block_true + thunk_true.basic_block_count();
self.blocks.insert(branch_block, BasicBlock::Branch {
block_true,
block_false,
});
self.blocks.insert(
branch_block,
BasicBlock::Branch {
block_true,
block_false,
},
);
self.flatten_next(next_block, *thunk_true);
self.flatten_next(next_block, *thunk_false);
assert_eq!(self.this_block(), next_block);
@@ -278,7 +286,10 @@ impl Visit for CompileBody<'_> {
if let Some(local) = self.compile.lookup_local(sym) {
thunk = Inst::PopLocal(Some(local)).into();
} else {
let global = self.compile.lookup_global(sym).expect("name expected to exist someplace(?)");
let global = self
.compile
.lookup_global(sym)
.expect("name expected to exist someplace(?)");
thunk = Inst::PopGlobal(Some(global)).into();
}
}
@@ -368,7 +379,7 @@ impl Visit for CompileBody<'_> {
// - push const
// (functions are unique const values so a new function will be created for every literal
// function defined in code)
// This is pretty much the only place where a new scope layer gets pushed beyond the start
// of the program
self.compile.push_scope_layer();
@@ -378,9 +389,7 @@ impl Visit for CompileBody<'_> {
}
// Compile function body
let mut code = self.visit_body(&expr.body)?
.flatten()
.to_vec();
let mut code = self.visit_body(&expr.body)?.flatten().to_vec();
// If the last instruction is not a return, or if there are no instructions, then return
// :nil value.
@@ -390,7 +399,9 @@ impl Visit for CompileBody<'_> {
}
// remap (Sym -> Name) to be (Name -> Sym) and make sure it's all in order.
let scope_locals: BTreeMap<_, _> = self.compile.pop_scope_layer()
let scope_locals: BTreeMap<_, _> = self
.compile
.pop_scope_layer()
.unwrap()
.into_iter()
.map(|(sym, name)| (name, sym))
@@ -399,7 +410,8 @@ impl Visit for CompileBody<'_> {
// this should be in numeric order since:
// 1. locals are created exactly once or looked up
// 2. scope_locals is a btreemap, keyed by names, which are in order from 0..N
let locals: FunLocals = scope_locals.into_iter()
let locals: FunLocals = scope_locals
.into_iter()
.enumerate()
.map(|(index, (name, sym))| {
assert_eq!(index, name.index());
@@ -407,7 +419,9 @@ impl Visit for CompileBody<'_> {
})
.collect();
let (hdl, _fun) = self.compile.push_const(UserFun::new_obj(code, locals, expr.params.len()));
let (hdl, _fun) =
self.compile
.push_const(UserFun::new_obj(code, locals, expr.params.len()));
// TODO(compile) : determine return value at the end of the body (preferably at parse-time)
@@ -427,7 +441,10 @@ impl Visit for CompileBody<'_> {
// This checks to make sure that it's both a local variable and that there's more
// than one scope layer.
let sym = global_sym(ident.to_string());
if let (true, Some(local)) = (self.compile.scope().layers_len() > 1, self.compile.lookup_local(sym)) {
if let (true, Some(local)) = (
self.compile.scope().layers_len() > 1,
self.compile.lookup_local(sym),
) {
// get local
Inst::LoadLocal(local).into()
} else {
@@ -466,25 +483,20 @@ fn test_flatten_thunk() {
let init_body = vec![
Inst::PushSym(Sym::new(0)),
Inst::PushSym(Sym::new(1)),
Inst::Call(1)
Inst::Call(1),
];
let true_body = vec![Inst::PushSym(Sym::new(2))];
let false_body = vec![Inst::PushSym(Sym::new(3))];
let end_body = vec![
Inst::PushSym(Sym::new(1)),
Inst::Call(1)
];
let end_body = vec![Inst::PushSym(Sym::new(1)), Inst::Call(1)];
let thunk = Thunk::List(vec![
// do something before
Thunk::Body(init_body.clone()),
// branch
Thunk::Branch {
thunk_true: Thunk::Body(true_body.clone()).into(),
thunk_false: Thunk::Body(false_body.clone()).into(),
},
// do something after
Thunk::Body(end_body.clone()),
]);
@@ -495,10 +507,55 @@ fn test_flatten_thunk() {
assert_eq!(blocks.len(), block_count);
let mut iter = blocks.into_iter();
assert_eq!(iter.next().unwrap(), (0, BasicBlock::Block { exit: 1, block: init_body, }));
assert_eq!(iter.next().unwrap(), (1, BasicBlock::Branch { block_true: 2, block_false: 3, }));
assert_eq!(iter.next().unwrap(), (2, BasicBlock::Block { exit: 4, block: true_body, }));
assert_eq!(iter.next().unwrap(), (3, BasicBlock::Block { exit: 4, block: false_body, }));
assert_eq!(iter.next().unwrap(), (4, BasicBlock::Block { exit: 5, block: end_body, }));
assert_eq!(
iter.next().unwrap(),
(
0,
BasicBlock::Block {
exit: 1,
block: init_body,
}
)
);
assert_eq!(
iter.next().unwrap(),
(
1,
BasicBlock::Branch {
block_true: 2,
block_false: 3,
}
)
);
assert_eq!(
iter.next().unwrap(),
(
2,
BasicBlock::Block {
exit: 4,
block: true_body,
}
)
);
assert_eq!(
iter.next().unwrap(),
(
3,
BasicBlock::Block {
exit: 4,
block: false_body,
}
)
);
assert_eq!(
iter.next().unwrap(),
(
4,
BasicBlock::Block {
exit: 5,
block: end_body,
}
)
);
assert!(iter.next().is_none());
}