Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit c994121

Browse files
committed
refactor binop parsing
1 parent a40d0e0 commit c994121

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/parse.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,37 @@ impl Tree {
4646
}
4747
}
4848
ParsedType::BinOp(binop) => {
49+
use rnix::types::BinOpKind::*;
4950
let left = recurse(binop.lhs().ok_or(EvalError::Parsing)?);
5051
let right = recurse(binop.rhs().ok_or(EvalError::Parsing)?);
51-
use rnix::types::BinOpKind::*;
52-
match binop.operator() {
52+
macro_rules! binop_source {
53+
( $op:expr ) => {
54+
TreeSource::BinOp {
55+
op: $op,
56+
left,
57+
right,
58+
}
59+
};
60+
}
61+
let op = match binop.operator() {
5362
And => TreeSource::BoolAnd { left, right },
5463
Or => TreeSource::BoolOr { left, right },
5564
Implication => TreeSource::Implication { left, right },
56-
_ => {
57-
let op = match binop.operator() {
58-
And | Or | IsSet | Implication => unreachable!(),
59-
Concat => BinOpKind::Concat,
60-
Update => BinOpKind::Update,
61-
Add => BinOpKind::Add,
62-
Sub => BinOpKind::Sub,
63-
Mul => BinOpKind::Mul,
64-
Div => BinOpKind::Div,
65-
Equal => BinOpKind::Equal,
66-
NotEqual => BinOpKind::NotEqual,
67-
Less => BinOpKind::Less,
68-
LessOrEq => BinOpKind::LessOrEq,
69-
More => BinOpKind::Greater,
70-
MoreOrEq => BinOpKind::GreaterOrEq,
71-
};
72-
TreeSource::BinOp { op, left, right }
73-
}
74-
}
65+
IsSet => return Err(EvalError::Unimplemented("IsSet".to_string())),
66+
Concat => binop_source!(BinOpKind::Concat),
67+
Update => binop_source!(BinOpKind::Update),
68+
Add => binop_source!(BinOpKind::Add),
69+
Sub => binop_source!(BinOpKind::Sub),
70+
Mul => binop_source!(BinOpKind::Mul),
71+
Div => binop_source!(BinOpKind::Div),
72+
Equal => binop_source!(BinOpKind::Equal),
73+
NotEqual => binop_source!(BinOpKind::NotEqual),
74+
Less => binop_source!(BinOpKind::Less),
75+
LessOrEq => binop_source!(BinOpKind::LessOrEq),
76+
More => binop_source!(BinOpKind::Greater),
77+
MoreOrEq => binop_source!(BinOpKind::GreaterOrEq),
78+
};
79+
TreeSource::BinOp { op, left, right }
7580
}
7681
ParsedType::UnaryOp(unary) => {
7782
use rnix::types::UnaryOpKind;

0 commit comments

Comments
 (0)