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

View File

@ -5,8 +5,8 @@ use std::{
use parity_wasm::elements::ValueType;
use wasm_ast::node::{
Backward, Br, BrIf, BrTable, Call, CallIndirect, Else, Forward, If, Intermediate, Return,
SetGlobal, SetLocal, SetTemporary, Statement, StoreAt,
Backward, Br, BrIf, BrTable, Call, CallIndirect, Forward, If, Intermediate, Return, SetGlobal,
SetLocal, SetTemporary, Statement, StoreAt,
};
use super::manager::{
@ -45,14 +45,6 @@ impl Driver for Backward {
}
}
impl Driver for Else {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
write!(w, "else ")?;
self.body.iter().try_for_each(|s| s.write(mng, w))
}
}
impl Driver for If {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
let label = mng.push_label();
@ -63,8 +55,10 @@ impl Driver for If {
self.truthy.iter().try_for_each(|s| s.write(mng, w))?;
if let Some(s) = &self.falsey {
s.write(mng, w)?;
if !self.falsey.is_empty() {
write!(w, "else ")?;
self.falsey.iter().try_for_each(|s| s.write(mng, w))?;
}
write!(w, "end ")?;

View File

@ -5,8 +5,8 @@ use std::{
use parity_wasm::elements::ValueType;
use wasm_ast::node::{
Backward, Br, BrIf, BrTable, Call, CallIndirect, Else, Forward, If, Intermediate, Return,
SetGlobal, SetLocal, SetTemporary, Statement, StoreAt,
Backward, Br, BrIf, BrTable, Call, CallIndirect, Forward, If, Intermediate, Return, SetGlobal,
SetLocal, SetTemporary, Statement, StoreAt,
};
use super::manager::{
@ -67,14 +67,6 @@ impl Driver for Backward {
}
}
impl Driver for Else {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
write!(w, "else ")?;
self.body.iter().try_for_each(|s| s.write(mng, w))
}
}
impl Driver for If {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
let rem = mng.push_label(Label::If);
@ -86,8 +78,10 @@ impl Driver for If {
self.truthy.iter().try_for_each(|s| s.write(mng, w))?;
if let Some(s) = &self.falsey {
s.write(mng, w)?;
if !self.falsey.is_empty() {
write!(w, "else ")?;
self.falsey.iter().try_for_each(|s| s.write(mng, w))?;
}
write!(w, "end ")?;

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,

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 {

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);
}