Remove verbose Else wrapper

This commit is contained in:
Rerumu
2022-06-10 03:21:16 -04:00
parent f85235738e
commit e0347c505e
5 changed files with 21 additions and 53 deletions
+4 -8
View File
@@ -4,7 +4,7 @@ use parity_wasm::elements::{
};
use crate::node::{
Backward, BinOp, BinOpType, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, CmpOpType, Else,
Backward, BinOp, BinOpType, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, CmpOpType,
Expression, Forward, GetGlobal, GetLocal, GetTemporary, If, Intermediate, LoadAt, LoadType,
MemoryGrow, MemorySize, Return, Select, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt,
StoreType, UnOp, UnOpType, Value,
@@ -605,18 +605,14 @@ impl<'a> Builder<'a> {
stat
}
fn new_else(&mut self, list: &mut &[Instruction]) -> Else {
Else {
body: self.new_stored_body(list),
}
}
fn new_if(&mut self, cond: Expression, list: &mut &[Instruction]) -> If {
let copied = <&[Instruction]>::clone(list);
let truthy = self.new_stored_body(list);
let end = copied.len() - list.len() - 1;
let falsey = is_else_stat(&copied[end]).then(|| self.new_else(list));
let falsey = is_else_stat(&copied[end])
.then(|| self.new_stored_body(list))
.unwrap_or_default();
If {
cond,
+1 -5
View File
@@ -653,14 +653,10 @@ pub struct Backward {
pub body: Vec<Statement>,
}
pub struct Else {
pub body: Vec<Statement>,
}
pub struct If {
pub cond: Expression,
pub truthy: Vec<Statement>,
pub falsey: Option<Else>,
pub falsey: Vec<Statement>,
}
pub struct Br {
+4 -16
View File
@@ -1,7 +1,7 @@
use crate::node::{
Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Else, Expression, Forward,
GetGlobal, GetLocal, GetTemporary, If, Intermediate, LoadAt, MemoryGrow, MemorySize, Return,
Select, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, UnOp, Value,
Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Expression, Forward, GetGlobal,
GetLocal, GetTemporary, If, Intermediate, LoadAt, MemoryGrow, MemorySize, Return, Select,
SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, UnOp, Value,
};
pub trait Visitor {
@@ -35,8 +35,6 @@ pub trait Visitor {
fn visit_backward(&mut self, _: &Backward) {}
fn visit_else(&mut self, _: &Else) {}
fn visit_if(&mut self, _: &If) {}
fn visit_br(&mut self, _: &Br) {}
@@ -188,16 +186,6 @@ impl<T: Visitor> Driver<T> for Backward {
}
}
impl<T: Visitor> Driver<T> for Else {
fn accept(&self, visitor: &mut T) {
for v in &self.body {
v.accept(visitor);
}
visitor.visit_else(self);
}
}
impl<T: Visitor> Driver<T> for If {
fn accept(&self, visitor: &mut T) {
self.cond.accept(visitor);
@@ -206,7 +194,7 @@ impl<T: Visitor> Driver<T> for If {
v.accept(visitor);
}
if let Some(v) = &self.falsey {
for v in &self.falsey {
v.accept(visitor);
}