summaryrefslogtreecommitdiff
path: root/src/types/operator.rs
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-03-06 20:33:27 +1300
committerBen Bridle <ben@derelict.engineering>2025-03-11 16:59:26 +1300
commit1ecee352f5844b0809d7ae66df52e34f42b44c8e (patch)
tree472b6fd57ff7f64ac3f8cd676cbe7a113ba01f05 /src/types/operator.rs
parentf2ed89083f5326a7a6f0a1720033d3388aa431fb (diff)
downloadtorque-asm-1ecee352f5844b0809d7ae66df52e34f42b44c8e.zip
Rewrite entire assembler
The language is now more general, the code is better structured, error reporting is more detailed, and many new language features have been implemented: - conditional blocks - first-class strings - more expression operators - binary literals - negative values - invocations in constant expressions
Diffstat (limited to 'src/types/operator.rs')
-rw-r--r--src/types/operator.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/types/operator.rs b/src/types/operator.rs
new file mode 100644
index 0000000..a7e7b9b
--- /dev/null
+++ b/src/types/operator.rs
@@ -0,0 +1,87 @@
+#[derive(Clone, Copy)]
+pub enum Operator {
+ Equal,
+ NotEqual,
+ LessThan,
+ GreaterThan,
+ LessThanEqual,
+ GreaterThanEqual,
+ Add,
+ Subtract,
+ Multiply,
+ Divide,
+ Modulo,
+ Exponent,
+ LeftShift,
+ RightShift,
+ BitAnd,
+ BitOr,
+ BitXor,
+ BitNot,
+}
+
+impl Operator {
+ pub fn from_str(string: &str) -> Option<Self> {
+ match string {
+ "=" => Some(Operator::Equal),
+ "==" => Some(Operator::Equal),
+ "<eq>" => Some(Operator::Equal),
+ "!=" => Some(Operator::NotEqual),
+ "<neq>" => Some(Operator::NotEqual),
+ "<" => Some(Operator::LessThan),
+ "<lth>" => Some(Operator::LessThan),
+ ">" => Some(Operator::GreaterThan),
+ "<gth>" => Some(Operator::GreaterThan),
+ "<=" => Some(Operator::LessThanEqual),
+ "<leq>" => Some(Operator::LessThanEqual),
+ ">=" => Some(Operator::GreaterThanEqual),
+ "<geq>" => Some(Operator::GreaterThanEqual),
+ "+" => Some(Operator::Add),
+ "<add>" => Some(Operator::Add),
+ "-" => Some(Operator::Subtract),
+ "<sub>" => Some(Operator::Subtract),
+ "*" => Some(Operator::Multiply),
+ "<mul>" => Some(Operator::Multiply),
+ "/" => Some(Operator::Divide),
+ "<div>" => Some(Operator::Divide),
+ "<mod>" => Some(Operator::Modulo),
+ "**" => Some(Operator::Exponent),
+ "<exp>" => Some(Operator::Exponent),
+ "<<" => Some(Operator::LeftShift),
+ "<shl>" => Some(Operator::LeftShift),
+ ">>" => Some(Operator::RightShift),
+ "<shr>" => Some(Operator::RightShift),
+ "<and>" => Some(Operator::BitAnd),
+ "<or>" => Some(Operator::BitOr),
+ "<xor>" => Some(Operator::BitXor),
+ "<not>" => Some(Operator::BitNot),
+ _ => None,
+ }
+ }
+}
+
+impl std::fmt::Display for Operator {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ let string = match self {
+ Operator::Equal => "<eq>",
+ Operator::NotEqual => "<neq>",
+ Operator::LessThan => "<lth>",
+ Operator::GreaterThan => "<gth>",
+ Operator::LessThanEqual => "<leq>",
+ Operator::GreaterThanEqual => "<geq>",
+ Operator::Add => "<add>",
+ Operator::Subtract => "<sub>",
+ Operator::Multiply => "<mul>",
+ Operator::Divide => "<div>",
+ Operator::Modulo => "<mod>",
+ Operator::Exponent => "<exp>",
+ Operator::LeftShift => "<shl>",
+ Operator::RightShift => "<shr>",
+ Operator::BitAnd => "<and>",
+ Operator::BitOr => "<or>",
+ Operator::BitXor => "<xor>",
+ Operator::BitNot => "<not>",
+ };
+ write!(f, "{string}")
+ }
+}