Skip to content

Commit

Permalink
[ast][hir] Operator -> BinaryOperator (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored Mar 17, 2024
1 parent 3db506b commit f2f7216
Show file tree
Hide file tree
Showing 31 changed files with 601 additions and 577 deletions.
38 changes: 19 additions & 19 deletions crates/samlang-ast/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl TypeDefinition {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Operator {
pub enum BinaryOperator {
MUL,
DIV,
MOD,
Expand All @@ -200,25 +200,25 @@ pub enum Operator {
NE,
}

impl Operator {
impl BinaryOperator {
pub fn as_str(&self) -> &'static str {
match self {
Operator::MUL => "*",
Operator::DIV => "/",
Operator::MOD => "%",
Operator::PLUS => "+",
Operator::MINUS => "-",
Operator::LAND => "&",
Operator::LOR => "|",
Operator::SHL => "<<",
Operator::SHR => ">>>",
Operator::XOR => "^",
Operator::LT => "<",
Operator::LE => "<=",
Operator::GT => ">",
Operator::GE => ">=",
Operator::EQ => "==",
Operator::NE => "!=",
BinaryOperator::MUL => "*",
BinaryOperator::DIV => "/",
BinaryOperator::MOD => "%",
BinaryOperator::PLUS => "+",
BinaryOperator::MINUS => "-",
BinaryOperator::LAND => "&",
BinaryOperator::LOR => "|",
BinaryOperator::SHL => "<<",
BinaryOperator::SHR => ">>>",
BinaryOperator::XOR => "^",
BinaryOperator::LT => "<",
BinaryOperator::LE => "<=",
BinaryOperator::GT => ">",
BinaryOperator::GE => ">=",
BinaryOperator::EQ => "==",
BinaryOperator::NE => "!=",
}
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@ impl Callee {
pub enum Statement {
Binary {
name: PStr,
operator: Operator,
operator: BinaryOperator,
e1: Expression,
e2: Expression,
},
Expand Down
48 changes: 24 additions & 24 deletions crates/samlang-ast/src/hir_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ mod tests {
std::cmp::Ordering::Equal
);
format!("{:?}", Expression::StringName(PStr::LOWER_A));
format!("{:?}", Operator::GE);
assert!(Operator::MINUS <= Operator::GE);
format!("{:?}", Operator::MINUS.partial_cmp(&Operator::GE));
format!("{:?}", Operator::MINUS.cmp(&Operator::GE));
format!("{:?}", BinaryOperator::GE);
assert!(BinaryOperator::MINUS <= BinaryOperator::GE);
format!("{:?}", BinaryOperator::MINUS.partial_cmp(&BinaryOperator::GE));
format!("{:?}", BinaryOperator::MINUS.cmp(&BinaryOperator::GE));
format!("{:?}", ZERO.type_());
format!("{:?}", Expression::StringName(PStr::LOWER_A).type_().as_id());
assert!(Expression::StringName(PStr::LOWER_A).type_().as_id().is_some());
Expand Down Expand Up @@ -128,7 +128,7 @@ mod tests {
assert!(Type::new_id(PStr::UPPER_A, vec![INT_TYPE, Type::new_id_no_targs(PStr::UPPER_B)])
.eq(&(Type::new_id(PStr::UPPER_A, vec![INT_TYPE, Type::new_id_no_targs(PStr::UPPER_B)]))));
let mut hasher = DefaultHasher::new();
Operator::DIV.hash(&mut hasher);
BinaryOperator::DIV.hash(&mut hasher);
Callee::FunctionName(FunctionNameExpression::new(
FunctionName { type_name: TypeName::new_for_test(PStr::UPPER_A), fn_name: PStr::LOWER_A },
FunctionType { argument_types: vec![], return_type: Box::new(INT_TYPE) },
Expand Down Expand Up @@ -221,67 +221,67 @@ mod tests {
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::LT,
operator: BinaryOperator::LT,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::LE,
operator: BinaryOperator::LE,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::GT,
operator: BinaryOperator::GT,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::GE,
operator: BinaryOperator::GE,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::EQ,
operator: BinaryOperator::EQ,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::NE,
operator: BinaryOperator::NE,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::LAND,
operator: BinaryOperator::LAND,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::LOR,
operator: BinaryOperator::LOR,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::SHL,
operator: BinaryOperator::SHL,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::SHR,
operator: BinaryOperator::SHR,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::XOR.clone(),
operator: BinaryOperator::XOR.clone(),
e1: ZERO,
e2: ZERO,
},
Expand All @@ -296,13 +296,13 @@ mod tests {
bindings: vec![None, Some((PStr::UNDERSCORE, INT_TYPE))],
s1: vec![Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::XOR.clone(),
operator: BinaryOperator::XOR.clone(),
e1: ZERO,
e2: ZERO,
}],
s2: vec![Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::XOR.clone(),
operator: BinaryOperator::XOR.clone(),
e1: ZERO,
e2: ZERO,
}],
Expand All @@ -312,37 +312,37 @@ mod tests {
s2: vec![
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::PLUS,
operator: BinaryOperator::PLUS,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::MINUS,
operator: BinaryOperator::MINUS,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::MINUS,
operator: BinaryOperator::MINUS,
e1: ZERO,
e2: Expression::int(-2147483648),
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::MUL,
operator: BinaryOperator::MUL,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::DIV,
operator: BinaryOperator::DIV,
e1: ZERO,
e2: ZERO,
},
Statement::Binary {
name: heap.alloc_str_for_test("dd"),
operator: Operator::MOD,
operator: BinaryOperator::MOD,
e1: ZERO,
e2: ZERO,
},
Expand Down
42 changes: 21 additions & 21 deletions crates/samlang-ast/src/lir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
hir::{GlobalVariable, Operator},
hir::{BinaryOperator, GlobalVariable},
mir::{FunctionName, SymbolTable, TypeNameId},
};
use enum_as_inner::EnumAsInner;
Expand Down Expand Up @@ -133,7 +133,7 @@ pub struct GenenalLoopVariable {
pub enum Statement {
Binary {
name: PStr,
operator: Operator,
operator: BinaryOperator,
e1: Expression,
e2: Expression,
},
Expand Down Expand Up @@ -192,10 +192,10 @@ pub enum Statement {
}

impl Statement {
pub fn binary(name: PStr, operator: Operator, e1: Expression, e2: Expression) -> Statement {
pub fn binary(name: PStr, operator: BinaryOperator, e1: Expression, e2: Expression) -> Statement {
match (operator, &e2) {
(Operator::MINUS, Expression::IntLiteral(n)) if *n != -2147483648 => {
Statement::Binary { name, operator: Operator::PLUS, e1, e2: Expression::int(-n) }
(BinaryOperator::MINUS, Expression::IntLiteral(n)) if *n != -2147483648 => {
Statement::Binary { name, operator: BinaryOperator::PLUS, e1, e2: Expression::int(-n) }
}
_ => Statement::Binary { name, operator, e1, e2 },
}
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Statement {
collector.push_str(name.as_str(heap));
collector.push_str(" = ");
match *operator {
Operator::DIV => {
BinaryOperator::DIV => {
// Necessary to preserve semantics
collector.push_str("Math.floor(");
e1.pretty_print(collector, heap, table);
Expand All @@ -248,12 +248,12 @@ impl Statement {
e2.pretty_print(collector, heap, table);
collector.push(')');
}
Operator::LT
| Operator::LE
| Operator::GT
| Operator::GE
| Operator::EQ
| Operator::NE => {
BinaryOperator::LT
| BinaryOperator::LE
| BinaryOperator::GT
| BinaryOperator::GE
| BinaryOperator::EQ
| BinaryOperator::NE => {
// Necessary to make TS happy
collector.push_str("Number(");
e1.pretty_print(collector, heap, table);
Expand All @@ -263,15 +263,15 @@ impl Statement {
e2.pretty_print(collector, heap, table);
collector.push(')');
}
Operator::MUL
| Operator::MOD
| Operator::PLUS
| Operator::MINUS
| Operator::LAND
| Operator::LOR
| Operator::SHL
| Operator::SHR
| Operator::XOR => {
BinaryOperator::MUL
| BinaryOperator::MOD
| BinaryOperator::PLUS
| BinaryOperator::MINUS
| BinaryOperator::LAND
| BinaryOperator::LOR
| BinaryOperator::SHL
| BinaryOperator::SHR
| BinaryOperator::XOR => {
e1.pretty_print(collector, heap, table);
collector.push(' ');
collector.push_str(operator.as_str());
Expand Down
33 changes: 19 additions & 14 deletions crates/samlang-ast/src/lir_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod tests {
use super::super::lir::*;
use crate::{
hir::{GlobalVariable, Operator},
hir::{BinaryOperator, GlobalVariable},
mir::{FunctionName, SymbolTable},
};
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -122,13 +122,18 @@ mod tests {
type_: Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))),
expression_list: vec![Expression::StringName(heap.alloc_str_for_test("meggo"))],
},
Statement::binary(heap.alloc_str_for_test("dd"), Operator::LT, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::LE, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::GT, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::GE, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::EQ, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::NE, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::XOR.clone(), ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::LT, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::LE, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::GT, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::GE, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::EQ, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::NE, ZERO, ZERO),
Statement::binary(
heap.alloc_str_for_test("dd"),
BinaryOperator::XOR.clone(),
ZERO,
ZERO,
),
Statement::While {
loop_variables: vec![],
statements: vec![Statement::SingleIf {
Expand All @@ -154,17 +159,17 @@ mod tests {
},
],
s2: vec![
Statement::binary(heap.alloc_str_for_test("dd"), Operator::PLUS, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::MINUS, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::PLUS, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::MINUS, ZERO, ZERO),
Statement::binary(
heap.alloc_str_for_test("dd"),
Operator::MINUS,
BinaryOperator::MINUS,
ZERO,
Expression::int(-2147483648),
),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::MUL, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::DIV, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), Operator::MOD, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::MUL, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::DIV, ZERO, ZERO),
Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::MOD, ZERO, ZERO),
Statement::Call {
callee: Expression::FnName(
FunctionName::new_for_test(heap.alloc_str_for_test("h")),
Expand Down
Loading

0 comments on commit f2f7216

Please sign in to comment.