Skip to content

Commit

Permalink
logical operators
Browse files Browse the repository at this point in the history
  • Loading branch information
metame committed Feb 2, 2024
1 parent c173430 commit c0e081f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ocaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub enum OCamlLiteral {
digits: String,
width: Option<usize>,
},
Boolean {
value: bool,
}
}

#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
Expand All @@ -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);
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/source_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ impl Display for OCamlLiteral {
write!(f, "{}", digits)
}
}
OCamlLiteral::Boolean { value} => {
write!(f, "{}", value)
}
}
}
}
Expand All @@ -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"),
}
}
Expand Down

0 comments on commit c0e081f

Please sign in to comment.