From c0e081f1bcfb8414054b99499d47dafe477991ba Mon Sep 17 00:00:00 2001 From: Michael J Erwin Date: Fri, 2 Feb 2024 14:54:04 -0500 Subject: [PATCH] logical operators --- src/ocaml.rs | 20 ++++++++++++++++++++ src/source_print.rs | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/src/ocaml.rs b/src/ocaml.rs index a19cf96..d34393f 100644 --- a/src/ocaml.rs +++ b/src/ocaml.rs @@ -84,6 +84,9 @@ pub enum OCamlLiteral { digits: String, width: Option, }, + Boolean { + value: bool, + } } #[derive(Debug, PartialEq, PartialOrd, Ord, Eq)] @@ -102,6 +105,8 @@ pub enum OCamlBinary { Multiply { left: OCamlExpr, right: OCamlExpr }, Divide { left: OCamlExpr, right: OCamlExpr }, Modulo { left: OCamlExpr, right: OCamlExpr }, + And { left: OCamlExpr, right: OCamlExpr }, + Or { left: OCamlExpr, right: OCamlExpr }, } struct SynPath<'a>(&'a syn::Path); @@ -170,6 +175,7 @@ impl From<&syn::Lit> for OCamlLiteral { match value { syn::Lit::Int(int) => int.into(), syn::Lit::Float(float) => float.into(), + syn::Lit::Bool(boolean) => boolean.into(), _ => todo!("{:#?} is not implemented", value), } } @@ -249,6 +255,12 @@ impl From<&syn::LitFloat> for OCamlLiteral { } } +impl From<&syn::LitBool> for OCamlLiteral { + fn from(value: &syn::LitBool) -> Self { + OCamlLiteral::Boolean { value: value.value } + } +} + impl From<&syn::ExprUnary> for OCamlUnary { fn from(value: &syn::ExprUnary) -> Self { match value.op { @@ -284,6 +296,14 @@ impl From<&syn::ExprBinary> for OCamlBinary { left: value.left.as_ref().into(), right: value.right.as_ref().into(), }, + syn::BinOp::And(_) => OCamlBinary::And { + left: value.left.as_ref().into(), + right: value.right.as_ref().into(), + }, + syn::BinOp::Or(_) => OCamlBinary::Or { + left: value.left.as_ref().into(), + right: value.right.as_ref().into(), + }, _ => unimplemented!("{:#?} is not implemented", value), } } diff --git a/src/source_print.rs b/src/source_print.rs index e878ae1..ccdfa75 100644 --- a/src/source_print.rs +++ b/src/source_print.rs @@ -86,6 +86,9 @@ impl Display for OCamlLiteral { write!(f, "{}", digits) } } + OCamlLiteral::Boolean { value} => { + write!(f, "{}", value) + } } } } @@ -108,6 +111,8 @@ impl std::fmt::Display for OCamlBinary { OCamlBinary::Multiply { left, right } => write!(f, "{left} * {right}"), OCamlBinary::Divide { left, right } => write!(f, "{left} / {right}"), OCamlBinary::Modulo { left, right } => write!(f, "{left} mod {right}"), + OCamlBinary::And { left, right } => write!(f, "{left} && {right}"), + OCamlBinary::Or { left, right } => write!(f, "{left} || {right}"), _ => todo!("something else again"), } }