diff --git a/codegen-luajit/src/analyzer/as_symbol.rs b/codegen-luajit/src/analyzer/as_symbol.rs new file mode 100644 index 0000000..b028d26 --- /dev/null +++ b/codegen-luajit/src/analyzer/as_symbol.rs @@ -0,0 +1,36 @@ +use wasm_ast::node::{BinOpType, CmpOpType}; + +pub trait AsSymbol { + fn as_symbol(&self) -> Option<&'static str>; +} + +impl AsSymbol for BinOpType { + fn as_symbol(&self) -> Option<&'static str> { + let result = match self { + Self::Add_I64 | Self::Add_FN => "+", + Self::Sub_I64 | Self::Sub_FN => "-", + Self::Mul_I64 | Self::Mul_FN => "*", + Self::DivS_I64 | Self::Div_FN => "/", + Self::RemS_I64 => "%", + _ => return None, + }; + + Some(result) + } +} + +impl AsSymbol for CmpOpType { + fn as_symbol(&self) -> Option<&'static str> { + let result = match self { + Self::Eq_I32 | Self::Eq_I64 | Self::Eq_FN => "==", + Self::Ne_I32 | Self::Ne_I64 | Self::Ne_FN => "~=", + Self::LtS_I32 | Self::LtS_I64 | Self::Lt_FN => "<", + Self::GtS_I32 | Self::GtS_I64 | Self::Gt_FN => ">", + Self::LeS_I32 | Self::LeS_I64 | Self::Le_FN => "<=", + Self::GeS_I32 | Self::GeS_I64 | Self::Ge_FN => ">=", + _ => return None, + }; + + Some(result) + } +} diff --git a/codegen-luajit/src/analyzer/localize.rs b/codegen-luajit/src/analyzer/localize.rs index 4fac567..3c33e4b 100644 --- a/codegen-luajit/src/analyzer/localize.rs +++ b/codegen-luajit/src/analyzer/localize.rs @@ -5,7 +5,7 @@ use wasm_ast::{ visit::{Driver, Visitor}, }; -use super::operator::{bin_symbol_of, cmp_symbol_of}; +use super::as_symbol::AsSymbol; struct Visit { local_set: BTreeSet<(&'static str, &'static str)>, @@ -34,7 +34,7 @@ impl Visitor for Visit { } fn visit_bin_op(&mut self, v: &BinOp) { - if bin_symbol_of(v.op_type()).is_some() { + if v.op_type().as_symbol().is_some() { return; } @@ -44,7 +44,7 @@ impl Visitor for Visit { } fn visit_cmp_op(&mut self, v: &CmpOp) { - if cmp_symbol_of(v.op_type()).is_some() { + if v.op_type().as_symbol().is_some() { return; } diff --git a/codegen-luajit/src/analyzer/mod.rs b/codegen-luajit/src/analyzer/mod.rs index 394a40c..4f71fce 100644 --- a/codegen-luajit/src/analyzer/mod.rs +++ b/codegen-luajit/src/analyzer/mod.rs @@ -1,3 +1,3 @@ +pub mod as_symbol; pub mod br_table; pub mod localize; -pub mod operator; diff --git a/codegen-luajit/src/analyzer/operator.rs b/codegen-luajit/src/analyzer/operator.rs deleted file mode 100644 index 64ce47b..0000000 --- a/codegen-luajit/src/analyzer/operator.rs +++ /dev/null @@ -1,28 +0,0 @@ -use wasm_ast::node::{BinOpType, CmpOpType}; - -pub fn bin_symbol_of(op: BinOpType) -> Option<&'static str> { - let result = match op { - BinOpType::Add_I64 | BinOpType::Add_FN => "+", - BinOpType::Sub_I64 | BinOpType::Sub_FN => "-", - BinOpType::Mul_I64 | BinOpType::Mul_FN => "*", - BinOpType::DivS_I64 | BinOpType::Div_FN => "/", - BinOpType::RemS_I64 => "%", - _ => return None, - }; - - Some(result) -} - -pub fn cmp_symbol_of(op: CmpOpType) -> Option<&'static str> { - let result = match op { - CmpOpType::Eq_I32 | CmpOpType::Eq_I64 | CmpOpType::Eq_FN => "==", - CmpOpType::Ne_I32 | CmpOpType::Ne_I64 | CmpOpType::Ne_FN => "~=", - CmpOpType::LtS_I32 | CmpOpType::LtS_I64 | CmpOpType::Lt_FN => "<", - CmpOpType::GtS_I32 | CmpOpType::GtS_I64 | CmpOpType::Gt_FN => ">", - CmpOpType::LeS_I32 | CmpOpType::LeS_I64 | CmpOpType::Le_FN => "<=", - CmpOpType::GeS_I32 | CmpOpType::GeS_I64 | CmpOpType::Ge_FN => ">=", - _ => return None, - }; - - Some(result) -} diff --git a/codegen-luajit/src/backend/expression.rs b/codegen-luajit/src/backend/expression.rs index bbc5419..31194ef 100644 --- a/codegen-luajit/src/backend/expression.rs +++ b/codegen-luajit/src/backend/expression.rs @@ -8,7 +8,7 @@ use wasm_ast::node::{ Value, }; -use crate::analyzer::operator::bin_symbol_of; +use crate::analyzer::as_symbol::AsSymbol; use super::manager::{ write_cmp_op, write_condition, write_separated, write_variable, Driver, Manager, @@ -103,7 +103,7 @@ impl Driver for UnOp { impl Driver for BinOp { fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> { - if let Some(symbol) = bin_symbol_of(self.op_type()) { + if let Some(symbol) = self.op_type().as_symbol() { write!(w, "(")?; self.lhs().write(mng, w)?; write!(w, "{symbol} ")?; diff --git a/codegen-luajit/src/backend/manager.rs b/codegen-luajit/src/backend/manager.rs index 237540c..c135608 100644 --- a/codegen-luajit/src/backend/manager.rs +++ b/codegen-luajit/src/backend/manager.rs @@ -6,7 +6,7 @@ use std::{ use wasm_ast::node::{BrTable, CmpOp, Expression}; -use crate::analyzer::operator::cmp_symbol_of; +use crate::analyzer::as_symbol::AsSymbol; #[derive(Default)] pub struct Manager { @@ -80,7 +80,7 @@ pub fn write_variable(var: usize, mng: &Manager, w: &mut dyn Write) -> Result<() } pub fn write_cmp_op(cmp: &CmpOp, mng: &mut Manager, w: &mut dyn Write) -> Result<()> { - if let Some(symbol) = cmp_symbol_of(cmp.op_type()) { + if let Some(symbol) = cmp.op_type().as_symbol() { cmp.lhs().write(mng, w)?; write!(w, "{symbol} ")?; cmp.rhs().write(mng, w) diff --git a/codegen-luau/src/analyzer/as_symbol.rs b/codegen-luau/src/analyzer/as_symbol.rs new file mode 100644 index 0000000..7270204 --- /dev/null +++ b/codegen-luau/src/analyzer/as_symbol.rs @@ -0,0 +1,36 @@ +use wasm_ast::node::{BinOpType, CmpOpType}; + +pub trait AsSymbol { + fn as_symbol(&self) -> Option<&'static str>; +} + +impl AsSymbol for BinOpType { + fn as_symbol(&self) -> Option<&'static str> { + let result = match self { + Self::Add_FN => "+", + Self::Sub_FN => "-", + Self::Mul_FN => "*", + Self::Div_FN => "/", + Self::RemS_I32 | Self::RemU_I32 => "%", + _ => return None, + }; + + Some(result) + } +} + +impl AsSymbol for CmpOpType { + fn as_symbol(&self) -> Option<&'static str> { + let result = match self { + Self::Eq_I32 | Self::Eq_FN => "==", + Self::Ne_I32 | Self::Ne_FN => "~=", + Self::LtU_I32 | Self::Lt_FN => "<", + Self::GtU_I32 | Self::Gt_FN => ">", + Self::LeU_I32 | Self::Le_FN => "<=", + Self::GeU_I32 | Self::Ge_FN => ">=", + _ => return None, + }; + + Some(result) + } +} diff --git a/codegen-luau/src/analyzer/localize.rs b/codegen-luau/src/analyzer/localize.rs index c9aab9e..2cf0587 100644 --- a/codegen-luau/src/analyzer/localize.rs +++ b/codegen-luau/src/analyzer/localize.rs @@ -6,7 +6,7 @@ use wasm_ast::{ visit::{Driver, Visitor}, }; -use super::operator::{bin_symbol_of, cmp_symbol_of}; +use super::as_symbol::AsSymbol; struct Visit { local_set: BTreeSet<(&'static str, &'static str)>, @@ -46,7 +46,7 @@ impl Visitor for Visit { } fn visit_bin_op(&mut self, v: &BinOp) { - if bin_symbol_of(v.op_type()).is_some() { + if v.op_type().as_symbol().is_some() { return; } @@ -56,7 +56,7 @@ impl Visitor for Visit { } fn visit_cmp_op(&mut self, v: &CmpOp) { - if cmp_symbol_of(v.op_type()).is_some() { + if v.op_type().as_symbol().is_some() { return; } diff --git a/codegen-luau/src/analyzer/mod.rs b/codegen-luau/src/analyzer/mod.rs index 394a40c..4f71fce 100644 --- a/codegen-luau/src/analyzer/mod.rs +++ b/codegen-luau/src/analyzer/mod.rs @@ -1,3 +1,3 @@ +pub mod as_symbol; pub mod br_table; pub mod localize; -pub mod operator; diff --git a/codegen-luau/src/analyzer/operator.rs b/codegen-luau/src/analyzer/operator.rs deleted file mode 100644 index 62830ef..0000000 --- a/codegen-luau/src/analyzer/operator.rs +++ /dev/null @@ -1,28 +0,0 @@ -use wasm_ast::node::{BinOpType, CmpOpType}; - -pub fn bin_symbol_of(op: BinOpType) -> Option<&'static str> { - let result = match op { - BinOpType::Add_FN => "+", - BinOpType::Sub_FN => "-", - BinOpType::Mul_FN => "*", - BinOpType::Div_FN => "/", - BinOpType::RemS_I32 | BinOpType::RemU_I32 => "%", - _ => return None, - }; - - Some(result) -} - -pub fn cmp_symbol_of(op: CmpOpType) -> Option<&'static str> { - let result = match op { - CmpOpType::Eq_I32 | CmpOpType::Eq_FN => "==", - CmpOpType::Ne_I32 | CmpOpType::Ne_FN => "~=", - CmpOpType::LtU_I32 | CmpOpType::Lt_FN => "<", - CmpOpType::GtU_I32 | CmpOpType::Gt_FN => ">", - CmpOpType::LeU_I32 | CmpOpType::Le_FN => "<=", - CmpOpType::GeU_I32 | CmpOpType::Ge_FN => ">=", - _ => return None, - }; - - Some(result) -} diff --git a/codegen-luau/src/backend/expression.rs b/codegen-luau/src/backend/expression.rs index 34572cb..de6b644 100644 --- a/codegen-luau/src/backend/expression.rs +++ b/codegen-luau/src/backend/expression.rs @@ -8,7 +8,7 @@ use wasm_ast::node::{ Value, }; -use crate::analyzer::operator::bin_symbol_of; +use crate::analyzer::as_symbol::AsSymbol; use super::manager::{ write_cmp_op, write_condition, write_separated, write_variable, Driver, Manager, @@ -123,7 +123,7 @@ impl Driver for UnOp { impl Driver for BinOp { fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> { - if let Some(symbol) = bin_symbol_of(self.op_type()) { + if let Some(symbol) = self.op_type().as_symbol() { write!(w, "(")?; self.lhs().write(mng, w)?; write!(w, "{symbol} ")?; diff --git a/codegen-luau/src/backend/manager.rs b/codegen-luau/src/backend/manager.rs index 1cc818d..182be4c 100644 --- a/codegen-luau/src/backend/manager.rs +++ b/codegen-luau/src/backend/manager.rs @@ -6,7 +6,7 @@ use std::{ use wasm_ast::node::{BrTable, CmpOp, Expression}; -use crate::analyzer::operator::cmp_symbol_of; +use crate::analyzer::as_symbol::AsSymbol; #[derive(PartialEq, Eq)] pub enum Label { @@ -84,7 +84,7 @@ pub fn write_variable(var: usize, mng: &Manager, w: &mut dyn Write) -> Result<() } pub fn write_cmp_op(cmp: &CmpOp, mng: &mut Manager, w: &mut dyn Write) -> Result<()> { - if let Some(symbol) = cmp_symbol_of(cmp.op_type()) { + if let Some(symbol) = cmp.op_type().as_symbol() { cmp.lhs().write(mng, w)?; write!(w, "{symbol} ")?; cmp.rhs().write(mng, w)