Skip to content

Commit 6632e89

Browse files
committed
add Expr type for union of BoolExpr or NumExpr
1 parent 7c81b30 commit 6632e89

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

argus-core/src/expr.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{ArgusResult, Error};
1717

1818
/// All expressions that are numeric
1919
#[derive(Clone, Debug, PartialEq, argus_derive::NumExpr)]
20-
#[enum_dispatch(Expr)]
20+
#[enum_dispatch(AnyExpr)]
2121
pub enum NumExpr {
2222
/// A signed integer literal
2323
IntLit(IntLit),
@@ -54,7 +54,7 @@ impl NumExpr {
5454

5555
/// All expressions that are evaluated to be of type `bool`
5656
#[derive(Clone, Debug, PartialEq, argus_derive::BoolExpr)]
57-
#[enum_dispatch(Expr)]
57+
#[enum_dispatch(AnyExpr)]
5858
pub enum BoolExpr {
5959
/// A `bool` literal
6060
BoolLit(BoolLit),
@@ -118,6 +118,15 @@ pub enum ExprRef<'a> {
118118
Num(&'a NumExpr),
119119
}
120120

121+
/// An expression (either [`BoolExpr`] or [`NumExpr`])
122+
#[derive(Clone, Debug, derive_more::From)]
123+
pub enum Expr {
124+
/// A reference to a [`BoolExpr`]
125+
Bool(BoolExpr),
126+
/// A reference to a [`NumExpr`]
127+
Num(NumExpr),
128+
}
129+
121130
/// Expression builder
122131
///
123132
/// The `ExprBuilder` is a factory structure that deals with the creation of

argus-core/src/expr/bool_expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::ops::{Bound, RangeBounds};
44
use std::time::Duration;
55

6-
use super::{BoolExpr, Expr, NumExpr};
6+
use super::{AnyExpr, BoolExpr, NumExpr};
77

88
/// Types of comparison operations
99
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -142,7 +142,7 @@ where
142142
// TODO(anand): Can I implement this within argus_derive?
143143
macro_rules! impl_bool_expr {
144144
($ty:ty$(, $($arg:ident),* )? ) => {
145-
impl Expr for $ty {
145+
impl AnyExpr for $ty {
146146
fn is_numeric(&self) -> bool {
147147
false
148148
}
@@ -157,7 +157,7 @@ macro_rules! impl_bool_expr {
157157
}
158158
};
159159
($ty:ty, [$args:ident]) => {
160-
impl Expr for $ty {
160+
impl AnyExpr for $ty {
161161
fn is_numeric(&self) -> bool {
162162
false
163163
}

argus-core/src/expr/iter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
33
use std::collections::VecDeque;
44

5-
use super::{Expr, ExprRef};
5+
use super::{AnyExpr, ExprRef};
66

7-
/// Iterator that starts from some root [`Expr`] and travels down to it's leaf
7+
/// Iterator that starts from some root [`AnyExpr`] and travels down to it's leaf
88
/// expressions.
99
///
1010
/// This essentially implements breadth-first search over the expression tree rooted at
11-
/// the given [`Expr`].
11+
/// the given [`AnyExpr`].
1212
pub struct AstIter<'a> {
1313
queue: VecDeque<ExprRef<'a>>,
1414
}
1515

1616
impl<'a> AstIter<'a> {
17-
/// Create an iterator that traverses an [`Expr`] from root to leaf.
17+
/// Create an iterator that traverses an [`AnyExpr`] from root to leaf.
1818
pub fn new(root: ExprRef<'a>) -> Self {
1919
let mut queue = VecDeque::new();
2020
queue.push_back(root);
@@ -28,7 +28,7 @@ impl<'a> Iterator for AstIter<'a> {
2828
fn next(&mut self) -> Option<Self::Item> {
2929
let expr_ref = self.queue.pop_front()?;
3030

31-
let expr: &dyn Expr = match expr_ref {
31+
let expr: &dyn AnyExpr = match expr_ref {
3232
ExprRef::Bool(expr) => expr,
3333
ExprRef::Num(expr) => expr,
3434
};

argus-core/src/expr/num_expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Numeric expression types
22
3-
use super::{Expr, NumExpr};
3+
use super::{AnyExpr, NumExpr};
44

55
// TODO(anand): Can I implement this within argus_derive?
66
macro_rules! impl_num_expr {
77
($ty:ty$(, $($arg:ident),* )? ) => {
8-
impl Expr for $ty {
8+
impl AnyExpr for $ty {
99
fn is_numeric(&self) -> bool {
1010
true
1111
}
@@ -20,7 +20,7 @@ macro_rules! impl_num_expr {
2020
}
2121
};
2222
($ty:ty, [$args:ident]) => {
23-
impl Expr for $ty {
23+
impl AnyExpr for $ty {
2424
fn is_numeric(&self) -> bool {
2525
false
2626
}

argus-core/src/expr/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{BoolExpr, ExprRef, NumExpr};
44

55
/// A trait representing expressions
66
#[enum_dispatch]
7-
pub trait Expr {
7+
pub trait AnyExpr {
88
/// Check if the given expression is a numeric expression
99
fn is_numeric(&self) -> bool;
1010
/// Check if the given expression is a boolean expression
@@ -17,7 +17,7 @@ pub trait Expr {
1717
}
1818

1919
/// Marker trait for numeric expressions
20-
pub trait IsNumExpr: Expr + Into<NumExpr> {}
20+
pub trait IsNumExpr: AnyExpr + Into<NumExpr> {}
2121

2222
/// Marker trait for Boolean expressions
23-
pub trait IsBoolExpr: Expr + Into<BoolExpr> {}
23+
pub trait IsBoolExpr: AnyExpr + Into<BoolExpr> {}

argus-parser/src/parser.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use std::{env, fmt, fs};
22

33
use ariadne::{sources, Color, Label, Report, ReportKind};
4-
use chumsky::{input::SpannedInput, prelude::*};
4+
use chumsky::input::SpannedInput;
5+
use chumsky::prelude::*;
56

6-
use crate::lexer::{lexer, Span, Token};
7+
use crate::lexer::{lexer, Error as LexError, Span, Token};
78

89
pub type Spanned<T> = (T, Span);
910

10-
// The type of the input that our parser operates on. The input is the `&[(Token, Span)]` token buffer generated by the
11-
// lexer, wrapped in a `SpannedInput` which 'splits' it apart into its constituent parts, tokens and spans, for chumsky
11+
// The type of the input that our parser operates on. The input is the `&[(Token,
12+
// Span)]` token buffer generated by the lexer, wrapped in a `SpannedInput` which
13+
// 'splits' it apart into its constituent parts, tokens and spans, for chumsky
1214
// to understand.
1315
type ParserInput<'tokens, 'src> = SpannedInput<Token<'src>, Span, &'tokens [(Token<'src>, Span)]>;
1416

1517
pub fn parser<'tokens, 'src: 'tokens>(
16-
) -> impl Parser<'tokens, ParserInput<'tokens, 'src>, Spanned<Expr<'src>>, extra::Err<Rich<'tokens, Token<'src>, Span>>>
17-
+ Clone {
18+
) -> impl Parser<'tokens, ParserInput<'tokens, 'src>, Spanned<Expr<'src>>, LexError<'src>> + Clone {
1819
}

0 commit comments

Comments
 (0)