Skip to content

Commit bbb53db

Browse files
committed
refactor: Make bi_tuple macro more expressive
1 parent 71cb432 commit bbb53db

3 files changed

Lines changed: 33 additions & 21 deletions

File tree

rusty_basic/src/instruction_generator/instruction_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl InstructionGenerator {
338338

339339
fn is_data_statement(statement: &Statement) -> bool {
340340
if let Statement::BuiltInSubCall(b) = statement {
341-
*b.left() == BuiltInSub::Data
341+
*b.built_in_sub() == BuiltInSub::Data
342342
} else {
343343
false
344344
}

rusty_parser/src/specific/core/macros.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
1+
// `bi_tuple` creates a tuple of two items.
2+
//
3+
// Example: `bi_tuple!(Person(name: String, age: int))`
4+
//
5+
// It implements the `From` trait to break apart the tuple into the members
6+
// or take references to them.
7+
//
8+
// It does not implement `AsRef` (or any other trait) per item type, as it might be that the two types are the same.
19
macro_rules! bi_tuple {
2-
($(#[$($attrss:tt)*])* $name: ident($left: ty, $right: ty)) => {
10+
($(#[$($attrss:tt)*])* $name: ident($left_name: tt: $left_type: ty, $right_name: tt: $right_type: ty)) => {
311
$(#[$($attrss)*])*
412
#[derive(Clone, Debug, PartialEq)]
5-
pub struct $name($left, $right);
13+
pub struct $name($left_type, $right_type);
614

715
impl $name {
8-
pub fn new(left: $left, right: $right) -> Self {
9-
Self(left, right)
16+
/// Creates a new instance of [$name].
17+
pub fn new($left_name: $left_type, $right_name: $right_type) -> Self {
18+
Self($left_name, $right_name)
1019
}
1120

21+
/// Tries to map the right side of the tuple with the given function.
1222
pub fn try_map_right<F, E>(self, f: F) -> Result<Self, E>
13-
where F : FnOnce($right) -> Result<$right, E>
23+
where F : FnOnce($right_type) -> Result<$right_type, E>
1424
{
15-
let (left, right) = self.into();
16-
f(right).map(|new_right| Self::new(left, new_right))
25+
let ($left_name, $right_name) = self.into();
26+
f($right_name).map(|new_right| Self::new($left_name, new_right))
1727
}
1828

19-
pub fn left(&self) -> &$left {
29+
/// Gets a reference to the left side member of the tuple.
30+
pub fn $left_name(&self) -> &$left_type {
2031
&self.0
2132
}
2233

23-
pub fn right(&self) -> &$right {
34+
/// Gets a reference to the right side member of the tuple.
35+
pub fn $right_name(&self) -> &$right_type {
2436
&self.1
2537
}
2638
}
2739

28-
impl From<$name> for ($left, $right) {
40+
// Break apart the tuple
41+
42+
impl From<$name> for ($left_type, $right_type) {
2943
fn from(value: $name) -> Self {
3044
(value.0, value.1)
3145
}
3246
}
3347

34-
impl<'a> From<&'a $name> for (&'a $left, &'a $right) {
48+
// Get references to both members of the tuple
49+
50+
impl<'a> From<&'a $name> for (&'a $left_type, &'a $right_type) {
3551
fn from(value: &'a $name) -> Self {
3652
(&value.0, &value.1)
3753
}

rusty_parser/src/specific/core/statement.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,22 @@ pub enum Statement {
141141

142142
bi_tuple!(
143143
/// A constant declaration.
144-
Constant(NamePos, ExpressionPos)
144+
Constant(name: NamePos, value: ExpressionPos)
145145
);
146146

147147
bi_tuple!(
148148
/// An assignment statement.
149-
Assignment(Expression, ExpressionPos)
149+
Assignment(lvalue: Expression, rvalue: ExpressionPos)
150150
);
151151

152152
bi_tuple!(
153153
/// A call to a user defined SUB.
154-
SubCall(BareName, Expressions)
154+
SubCall(sub_name: BareName, args: Expressions)
155155
);
156156

157157
bi_tuple!(
158158
/// A call to a built-in SUB.
159-
BuiltInSubCall(BuiltInSub, Expressions)
159+
BuiltInSubCall(built_in_sub: BuiltInSub, args: Expressions)
160160
);
161161

162162
/// A list of variables defined in a DIM statement.
@@ -228,14 +228,10 @@ pub struct SelectCase {
228228
bi_tuple!(
229229
/// A case block can have one or more condition expressions and
230230
/// the statements to execute if the condition is met.
231-
CaseBlock(Vec<CaseExpression>, Statements)
231+
CaseBlock(conditions: Vec<CaseExpression>, statements: Statements)
232232
);
233233

234234
impl CaseBlock {
235-
pub fn conditions(&self) -> &Vec<CaseExpression> {
236-
&self.0
237-
}
238-
239235
pub fn has_conditions(&self) -> bool {
240236
// the CASE ELSE block does not have conditions
241237
!self.conditions().is_empty()

0 commit comments

Comments
 (0)