Refactor symbol usage into AsSymbol
This commit is contained in:
parent
9db21cc84b
commit
2c6176d538
36
codegen-luajit/src/analyzer/as_symbol.rs
Normal file
36
codegen-luajit/src/analyzer/as_symbol.rs
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
pub mod as_symbol;
|
||||
pub mod br_table;
|
||||
pub mod localize;
|
||||
pub mod operator;
|
||||
|
@ -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)
|
||||
}
|
@ -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} ")?;
|
||||
|
@ -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)
|
||||
|
36
codegen-luau/src/analyzer/as_symbol.rs
Normal file
36
codegen-luau/src/analyzer/as_symbol.rs
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
pub mod as_symbol;
|
||||
pub mod br_table;
|
||||
pub mod localize;
|
||||
pub mod operator;
|
||||
|
@ -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)
|
||||
}
|
@ -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} ")?;
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user