Skip to content

Commit 17f5856

Browse files
committed
refactor: Changed Statement::BuiltInSubCall to use a struct instead of two fields
1 parent 3aab976 commit 17f5856

29 files changed

Lines changed: 121 additions & 101 deletions

rusty_basic/src/instruction_generator/calls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ impl InstructionGenerator {
2222

2323
pub fn generate_built_in_sub_call_instructions(
2424
&mut self,
25-
name: BuiltInSub,
26-
args: Expressions,
25+
built_in_sub_call: BuiltInSubCall,
2726
pos: Position,
2827
) {
28+
let (name, args) = built_in_sub_call.into();
2929
self.generate_push_unnamed_args_instructions(&args, pos);
3030
self.push(Instruction::PushStack, pos);
3131
self.push(Instruction::BuiltInSub(name), pos);

rusty_basic/src/instruction_generator/instruction_generator.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl InstructionGenerator {
326326
let mut data_statements: Statements = vec![];
327327
let mut other_statements: Statements = vec![];
328328
for statement in statements {
329-
if let Statement::BuiltInSubCall(BuiltInSub::Data, _) = &statement.element {
329+
if Self::is_data_statement(&statement.element) {
330330
data_statements.push(statement);
331331
} else {
332332
other_statements.push(statement);
@@ -336,6 +336,14 @@ impl InstructionGenerator {
336336
data_statements
337337
}
338338

339+
fn is_data_statement(statement: &Statement) -> bool {
340+
if let Statement::BuiltInSubCall(b) = statement {
341+
*b.left() == BuiltInSub::Data
342+
} else {
343+
false
344+
}
345+
}
346+
339347
fn visit_global_statements(&mut self, statements: Statements) {
340348
self.visit(statements);
341349

rusty_basic/src/instruction_generator/statement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl Visitor<StatementPos> for InstructionGenerator {
3030
// with their actual value.
3131
}
3232
Statement::SubCall(sub_call) => self.generate_sub_call_instructions(sub_call, pos),
33-
Statement::BuiltInSubCall(n, args) => {
34-
self.generate_built_in_sub_call_instructions(n, args, pos)
33+
Statement::BuiltInSubCall(sub_call) => {
34+
self.generate_built_in_sub_call_instructions(sub_call, pos)
3535
}
3636
Statement::Print(print) => {
3737
self.generate_print_instructions(print, pos);

rusty_linter/src/converter/statement/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ impl ConvertibleIn<Position> for Statement {
1515
// CONST is mapped to None and is filtered out
1616
Self::Const(c) => const_rules::on_const(ctx, c),
1717
Self::SubCall(sub_call) => ctx.sub_call(sub_call),
18-
Self::BuiltInSubCall(built_in_sub, args) => {
18+
Self::BuiltInSubCall(sub_call) => {
19+
let (built_in_sub, args) = sub_call.into();
1920
let converted_args = args.convert_in(ctx, ExprContext::Argument)?;
20-
Ok(Self::BuiltInSubCall(built_in_sub, converted_args))
21+
Ok(Self::built_in_sub_call(built_in_sub, converted_args))
2122
}
2223
Self::IfBlock(i) => i.convert(ctx).map(Statement::IfBlock),
2324
Self::SelectCase(s) => s.convert(ctx).map(Statement::SelectCase),

rusty_linter/src/converter/statement/sub_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Context {
1010
let converted_args = args.convert_in(self, ExprContext::Argument)?;
1111
let opt_built_in: Option<BuiltInSub> = BuiltInSub::parse_non_keyword_sub(sub_name.as_ref());
1212
match opt_built_in {
13-
Some(b) => Ok(Statement::BuiltInSubCall(b, converted_args)),
13+
Some(b) => Ok(Statement::built_in_sub_call(b, converted_args)),
1414
None => Ok(Statement::sub_call(sub_name, converted_args)),
1515
}
1616
}

rusty_linter/src/post_linter/built_in_linter.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use super::post_conversion_linter::PostConversionLinter;
22
use crate::built_ins::{lint_function_call, lint_sub_call};
3-
use crate::core::LintErrorPos;
4-
use crate::core::NameContext;
3+
use crate::core::{LintErrorPos, NameContext};
54
use rusty_common::*;
6-
use rusty_parser::BuiltInSub;
7-
use rusty_parser::{
8-
Expression, ExpressionPos, Expressions, FunctionImplementation, SubImplementation,
9-
};
5+
use rusty_parser::*;
106

117
/// Lints built-in functions and subs.
128
pub struct BuiltInLinter {
@@ -41,10 +37,10 @@ impl PostConversionLinter for BuiltInLinter {
4137

4238
fn visit_built_in_sub_call(
4339
&mut self,
44-
built_in_sub: &BuiltInSub,
40+
built_in_sub_call: &BuiltInSubCall,
4541
pos: Position,
46-
args: &Expressions,
4742
) -> Result<(), LintErrorPos> {
43+
let (built_in_sub, args) = built_in_sub_call.into();
4844
self.visit_expressions(args)?;
4945
lint_sub_call(built_in_sub, pos, args, self.name_context)
5046
}

rusty_linter/src/post_linter/expression_reducer.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::core::LintErrorPos;
22
use rusty_common::*;
3-
use rusty_parser::BuiltInSub;
43
use rusty_parser::*;
54

65
/// Visits the converted program and transforms it into a different program.
@@ -108,15 +107,10 @@ pub trait ExpressionReducer {
108107
// any further.
109108
Ok(Statement::Const(c))
110109
}
111-
Statement::SubCall(s) => self.visit_sub_call(s).map(|(reduced_name, reduced_expr)| {
112-
Statement::sub_call(reduced_name, reduced_expr)
113-
}),
114-
Statement::BuiltInSubCall(b, e) => {
115-
self.visit_built_in_sub_call(b, e)
116-
.map(|(reduced_name, reduced_expr)| {
117-
Statement::BuiltInSubCall(reduced_name, reduced_expr)
118-
})
119-
}
110+
Statement::SubCall(s) => self.visit_sub_call(s).map(Statement::SubCall),
111+
Statement::BuiltInSubCall(s) => self
112+
.visit_built_in_sub_call(s)
113+
.map(Statement::BuiltInSubCall),
120114
Statement::Print(p) => self.visit_print(p).map(Statement::Print),
121115
Statement::IfBlock(i) => self.visit_if_block(i).map(Statement::IfBlock),
122116
Statement::SelectCase(s) => self.visit_select_case(s).map(Statement::SelectCase),
@@ -138,20 +132,15 @@ pub trait ExpressionReducer {
138132
}
139133
}
140134

141-
fn visit_sub_call(
142-
&mut self,
143-
sub_call: SubCall,
144-
) -> Result<(CaseInsensitiveString, Expressions), LintErrorPos> {
145-
let (name, args) = sub_call.into();
146-
Ok((name, self.visit_expressions(args)?))
135+
fn visit_sub_call(&mut self, sub_call: SubCall) -> Result<SubCall, LintErrorPos> {
136+
sub_call.try_map_right(|args| self.visit_expressions(args))
147137
}
148138

149139
fn visit_built_in_sub_call(
150140
&mut self,
151-
name: BuiltInSub,
152-
args: Expressions,
153-
) -> Result<(BuiltInSub, Expressions), LintErrorPos> {
154-
Ok((name, self.visit_expressions(args)?))
141+
sub_call: BuiltInSubCall,
142+
) -> Result<BuiltInSubCall, LintErrorPos> {
143+
sub_call.try_map_right(|args| self.visit_expressions(args))
155144
}
156145

157146
fn visit_assignment(&mut self, a: Assignment) -> Result<Assignment, LintErrorPos> {

rusty_linter/src/post_linter/post_conversion_linter.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::core::LintErrorPos;
22
use rusty_common::*;
3-
use rusty_parser::BuiltInSub;
43
use rusty_parser::*;
54

65
/// Invoked after the conversion to fully typed program.
@@ -58,7 +57,7 @@ pub trait PostConversionLinter {
5857
match s {
5958
Statement::Assignment(a) => self.visit_assignment(a, pos),
6059
Statement::SubCall(sub_call) => self.visit_sub_call(sub_call, pos),
61-
Statement::BuiltInSubCall(b, e) => self.visit_built_in_sub_call(b, pos, e),
60+
Statement::BuiltInSubCall(sub_call) => self.visit_built_in_sub_call(sub_call, pos),
6261
Statement::IfBlock(i) => self.visit_if_block(i),
6362
Statement::SelectCase(s) => self.visit_select_case(s),
6463
Statement::ForLoop(f) => self.visit_for_loop(f),
@@ -145,10 +144,10 @@ pub trait PostConversionLinter {
145144

146145
fn visit_built_in_sub_call(
147146
&mut self,
148-
_name: &BuiltInSub,
147+
sub_call: &BuiltInSubCall,
149148
_pos: Position,
150-
args: &Expressions,
151149
) -> Result<(), LintErrorPos> {
150+
let (_, args) = sub_call.into();
152151
self.visit_expressions(args)
153152
}
154153

rusty_parser/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ mod tests {
7575
"Enter the number of fibonacci to calculate".as_lit_expr(2, 7)
7676
))),
7777
// INPUT N
78-
GlobalStatement::Statement(Statement::BuiltInSubCall(
78+
GlobalStatement::Statement(Statement::built_in_sub_call(
7979
BuiltInSub::Input,
8080
vec![
8181
0.as_lit_expr(1, 1), // no file number

rusty_parser/src/specific/built_ins/close.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn parse() -> impl Parser<RcStringView, Output = Statement> {
1313
seq2(
1414
keyword(Keyword::Close),
1515
file_handles(),
16-
|_, file_handles| Statement::BuiltInSubCall(BuiltInSub::Close, file_handles),
16+
|_, file_handles| Statement::built_in_sub_call(BuiltInSub::Close, file_handles),
1717
)
1818
}
1919

@@ -49,7 +49,7 @@ mod tests {
4949
let statement = parse(input).demand_single_statement();
5050
assert_eq!(
5151
statement,
52-
Statement::BuiltInSubCall(BuiltInSub::Close, vec![])
52+
Statement::built_in_sub_call(BuiltInSub::Close, vec![])
5353
);
5454
}
5555

@@ -59,7 +59,7 @@ mod tests {
5959
let statement = parse(input).demand_single_statement();
6060
assert_eq!(
6161
statement,
62-
Statement::BuiltInSubCall(BuiltInSub::Close, vec![1.as_lit_expr(1, 7)])
62+
Statement::built_in_sub_call(BuiltInSub::Close, vec![1.as_lit_expr(1, 7)])
6363
);
6464
}
6565

@@ -76,7 +76,7 @@ mod tests {
7676
let statement = parse(input).demand_single_statement();
7777
assert_eq!(
7878
statement,
79-
Statement::BuiltInSubCall(
79+
Statement::built_in_sub_call(
8080
BuiltInSub::Close,
8181
vec![Expression::Parenthesis(Box::new(1.as_lit_expr(1, 8))).at_rc(1, 7)]
8282
)
@@ -89,7 +89,7 @@ mod tests {
8989
let statement = parse(input).demand_single_statement();
9090
assert_eq!(
9191
statement,
92-
Statement::BuiltInSubCall(
92+
Statement::built_in_sub_call(
9393
BuiltInSub::Close,
9494
vec![Expression::Parenthesis(Box::new(1.as_lit_expr(1, 7))).at_rc(1, 6)]
9595
)
@@ -102,7 +102,7 @@ mod tests {
102102
let statement = parse(input).demand_single_statement();
103103
assert_eq!(
104104
statement,
105-
Statement::BuiltInSubCall(BuiltInSub::Close, vec![1.as_lit_expr(1, 7)])
105+
Statement::built_in_sub_call(BuiltInSub::Close, vec![1.as_lit_expr(1, 7)])
106106
);
107107
}
108108

@@ -140,7 +140,7 @@ mod tests {
140140
let statement = parse(input).demand_single_statement();
141141
assert_eq!(
142142
statement,
143-
Statement::BuiltInSubCall(
143+
Statement::built_in_sub_call(
144144
BuiltInSub::Close,
145145
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 10)]
146146
)
@@ -153,7 +153,7 @@ mod tests {
153153
let statement = parse(input).demand_single_statement();
154154
assert_eq!(
155155
statement,
156-
Statement::BuiltInSubCall(
156+
Statement::built_in_sub_call(
157157
BuiltInSub::Close,
158158
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 10)]
159159
)
@@ -166,7 +166,7 @@ mod tests {
166166
let statement = parse(input).demand_single_statement();
167167
assert_eq!(
168168
statement,
169-
Statement::BuiltInSubCall(
169+
Statement::built_in_sub_call(
170170
BuiltInSub::Close,
171171
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 11)]
172172
)
@@ -179,7 +179,7 @@ mod tests {
179179
let statement = parse(input).demand_single_statement();
180180
assert_eq!(
181181
statement,
182-
Statement::BuiltInSubCall(
182+
Statement::built_in_sub_call(
183183
BuiltInSub::Close,
184184
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 9)]
185185
)
@@ -192,7 +192,7 @@ mod tests {
192192
let statement = parse(input).demand_single_statement();
193193
assert_eq!(
194194
statement,
195-
Statement::BuiltInSubCall(
195+
Statement::built_in_sub_call(
196196
BuiltInSub::Close,
197197
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 11)]
198198
)
@@ -205,7 +205,7 @@ mod tests {
205205
let statement = parse(input).demand_single_statement();
206206
assert_eq!(
207207
statement,
208-
Statement::BuiltInSubCall(
208+
Statement::built_in_sub_call(
209209
BuiltInSub::Close,
210210
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 11)]
211211
)
@@ -218,7 +218,7 @@ mod tests {
218218
let statement = parse(input).demand_single_statement();
219219
assert_eq!(
220220
statement,
221-
Statement::BuiltInSubCall(
221+
Statement::built_in_sub_call(
222222
BuiltInSub::Close,
223223
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 12)]
224224
)
@@ -231,7 +231,7 @@ mod tests {
231231
let statement = parse(input).demand_single_statement();
232232
assert_eq!(
233233
statement,
234-
Statement::BuiltInSubCall(
234+
Statement::built_in_sub_call(
235235
BuiltInSub::Close,
236236
vec![1.as_lit_expr(1, 7), 2.as_lit_expr(1, 10)]
237237
)
@@ -245,7 +245,7 @@ mod tests {
245245
assert_eq!(
246246
program,
247247
vec![
248-
GlobalStatement::Statement(Statement::BuiltInSubCall(
248+
GlobalStatement::Statement(Statement::built_in_sub_call(
249249
BuiltInSub::Close,
250250
vec![1.as_lit_expr(1, 7)]
251251
))

0 commit comments

Comments
 (0)