Skip to content

Commit

Permalink
add infix and and or operators
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrolarus committed Mar 10, 2021
1 parent d9abe17 commit b4e5ab8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cytosol-hir/src/ast_to_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,9 @@ impl Translator<'_> {
InfixOperator::Lte => vec![binop!(int, int => bool)],
InfixOperator::Gt => vec![binop!(int, int => bool)],
InfixOperator::Gte => vec![binop!(int, int => bool)],

InfixOperator::And => vec![binop!(bool, bool => bool)],
InfixOperator::Or => vec![binop!(bool, bool => bool)],
};

if let Some((_, res_ty)) = operator_types
Expand Down
5 changes: 5 additions & 0 deletions cytosol-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ pub enum TokenKind<'src> {
#[token("≥")]
OpGreaterThanEqual,

#[token("and")]
OpAnd,
#[token("or")]
OpOr,

#[error]
// skip whitespace
#[regex(r"[ \t\n\f]+", logos::skip)]
Expand Down
2 changes: 2 additions & 0 deletions cytosol-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ impl<'src, I: Iterator<Item = Token<'src>>> Parser<'src, I> {
TokenKind::OpLessThanEqual => (next.fc, InfixOperator::Lte),
TokenKind::OpGreaterThan => (next.fc, InfixOperator::Gt),
TokenKind::OpGreaterThanEqual => (next.fc, InfixOperator::Gte),
TokenKind::OpAnd => (next.fc, InfixOperator::And),
TokenKind::OpOr => (next.fc, InfixOperator::Or),
_ => return Ok(expr),
};

Expand Down
3 changes: 3 additions & 0 deletions cytosol-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ fn eval_expr(
(Gt, Integer(a), Integer(b)) => Some(Bool(a > b)),
(Gte, Integer(a), Integer(b)) => Some(Bool(a >= b)),

(And, Bool(a), Bool(b)) => Some(Bool(a && b)),
(Or, Bool(a), Bool(b)) => Some(Bool(a || b)),

_ => None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions cytosol-syntax/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,7 @@ pub enum InfixOperator {
Lte,
Gt,
Gte,

And,
Or,
}
2 changes: 2 additions & 0 deletions cytosol-tester/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ impl ToDoc for InfixOperator {
InfixOperator::Lte => Doc::text("≤"),
InfixOperator::Gt => Doc::text(">"),
InfixOperator::Gte => Doc::text("≥"),
InfixOperator::And => Doc::text("and"),
InfixOperator::Or => Doc::text("or"),
}
}
}
Expand Down

0 comments on commit b4e5ab8

Please sign in to comment.