Factor out operator analysis and simplify output

This commit is contained in:
Rerumu
2022-04-29 23:30:55 -04:00
parent 2e5890f466
commit 7913dd507a
14 changed files with 121 additions and 92 deletions
+15 -23
View File
@@ -5,6 +5,8 @@ use wasm_ast::node::{
UnOp, Value,
};
use crate::analyzer::operator::bin_symbol_of;
use super::manager::{write_f32, write_f64, write_separated, write_variable, Driver, Manager};
impl Driver for Recall {
@@ -80,32 +82,22 @@ impl Driver for UnOp {
}
}
fn write_bin_op(bin_op: &BinOp, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
let op = bin_op.op.as_operator().unwrap();
write!(w, "(")?;
bin_op.lhs.write(mng, w)?;
write!(w, "{op} ")?;
bin_op.rhs.write(mng, w)?;
write!(w, ")")
}
fn write_bin_op_call(bin_op: &BinOp, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
let (a, b) = bin_op.op.as_name();
write!(w, "{a}_{b}(")?;
bin_op.lhs.write(mng, w)?;
write!(w, ", ")?;
bin_op.rhs.write(mng, w)?;
write!(w, ")")
}
impl Driver for BinOp {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
if self.op.as_operator().is_some() {
write_bin_op(self, mng, w)
if let Some(symbol) = bin_symbol_of(self.op) {
write!(w, "(")?;
self.lhs.write(mng, w)?;
write!(w, "{symbol} ")?;
self.rhs.write(mng, w)?;
write!(w, ")")
} else {
write_bin_op_call(self, mng, w)
let (head, tail) = self.op.as_name();
write!(w, "{head}_{tail}(")?;
self.lhs.write(mng, w)?;
write!(w, ", ")?;
self.rhs.write(mng, w)?;
write!(w, ")")
}
}
}
+2 -2
View File
@@ -259,14 +259,14 @@ fn write_variable_list(ir: &Intermediate, w: &mut dyn Write) -> Result<()> {
for data in &ir.local_data {
let range = total..total + usize::try_from(data.count()).unwrap();
let typed = data.value_type();
total = range.end;
write!(w, "local ")?;
write_ascending("loc", range.clone(), w)?;
write!(w, " = ")?;
write_separated(range, |_, w| write!(w, "ZERO_{typed} "), w)?;
write_separated(range, |_, w| w.write_all(b"0"), w)?;
write!(w, " ")?;
}
if ir.num_stack != 0 {