Skip to content

Commit 3aab976

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

19 files changed

Lines changed: 81 additions & 81 deletions

File tree

rusty_basic/src/instruction_generator/calls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl InstructionGenerator {
6666
self.generate_un_stash_function_return_value(pos);
6767
}
6868

69-
pub fn generate_sub_call_instructions(&mut self, name_pos: BareNamePos, args: Expressions) {
70-
let Positioned { element: name, pos } = name_pos;
69+
pub fn generate_sub_call_instructions(&mut self, sub_call: SubCall, pos: Position) {
70+
let (name, args) = sub_call.into();
7171
let subprogram_name = SubprogramName::Sub(name);
7272
// cloning to fight the borrow checker
7373
let sub_impl_parameters: Vec<Parameter> = self

rusty_basic/src/instruction_generator/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Visitor<StatementPos> for InstructionGenerator {
2929
// because the linter has replaced expressions that reference constants
3030
// with their actual value.
3131
}
32-
Statement::SubCall(n, args) => self.generate_sub_call_instructions(n.at_pos(pos), args),
32+
Statement::SubCall(sub_call) => self.generate_sub_call_instructions(sub_call, pos),
3333
Statement::BuiltInSubCall(n, args) => {
3434
self.generate_built_in_sub_call_instructions(n, args, pos)
3535
}

rusty_linter/src/converter/statement/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl ConvertibleIn<Position> for Statement {
1414
Self::Assignment(a) => assignment::on_assignment(a, ctx, pos),
1515
// CONST is mapped to None and is filtered out
1616
Self::Const(c) => const_rules::on_const(ctx, c),
17-
Self::SubCall(n, args) => ctx.sub_call(n, args),
17+
Self::SubCall(sub_call) => ctx.sub_call(sub_call),
1818
Self::BuiltInSubCall(built_in_sub, args) => {
1919
let converted_args = args.convert_in(ctx, ExprContext::Argument)?;
2020
Ok(Self::BuiltInSubCall(built_in_sub, converted_args))

rusty_linter/src/converter/statement/sub_call.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@ use crate::converter::common::Context;
22
use crate::converter::common::ConvertibleIn;
33
use crate::converter::common::ExprContext;
44
use crate::core::LintErrorPos;
5-
use rusty_parser::{
6-
BuiltInSub, {BareName, Expressions, Statement},
7-
};
5+
use rusty_parser::{BuiltInSub, Statement, SubCall};
86

97
impl Context {
10-
pub fn sub_call(
11-
&mut self,
12-
sub_name: BareName,
13-
args: Expressions,
14-
) -> Result<Statement, LintErrorPos> {
8+
pub fn sub_call(&mut self, sub_call: SubCall) -> Result<Statement, LintErrorPos> {
9+
let (sub_name, args) = sub_call.into();
1510
let converted_args = args.convert_in(self, ExprContext::Argument)?;
1611
let opt_built_in: Option<BuiltInSub> = BuiltInSub::parse_non_keyword_sub(sub_name.as_ref());
1712
match opt_built_in {
1813
Some(b) => Ok(Statement::BuiltInSubCall(b, converted_args)),
19-
None => Ok(Statement::SubCall(sub_name, converted_args)),
14+
None => Ok(Statement::sub_call(sub_name, converted_args)),
2015
}
2116
}
2217
}

rusty_linter/src/post_linter/expression_reducer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ pub trait ExpressionReducer {
108108
// any further.
109109
Ok(Statement::Const(c))
110110
}
111-
Statement::SubCall(b, e) => self
112-
.visit_sub_call(b, e)
113-
.map(|(reduced_name, reduced_expr)| Statement::SubCall(reduced_name, reduced_expr)),
111+
Statement::SubCall(s) => self.visit_sub_call(s).map(|(reduced_name, reduced_expr)| {
112+
Statement::sub_call(reduced_name, reduced_expr)
113+
}),
114114
Statement::BuiltInSubCall(b, e) => {
115115
self.visit_built_in_sub_call(b, e)
116116
.map(|(reduced_name, reduced_expr)| {
@@ -140,9 +140,9 @@ pub trait ExpressionReducer {
140140

141141
fn visit_sub_call(
142142
&mut self,
143-
name: CaseInsensitiveString,
144-
args: Expressions,
143+
sub_call: SubCall,
145144
) -> Result<(CaseInsensitiveString, Expressions), LintErrorPos> {
145+
let (name, args) = sub_call.into();
146146
Ok((name, self.visit_expressions(args)?))
147147
}
148148

rusty_linter/src/post_linter/post_conversion_linter.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait PostConversionLinter {
5757
fn visit_statement_pos(&mut self, s: &Statement, pos: Position) -> Result<(), LintErrorPos> {
5858
match s {
5959
Statement::Assignment(a) => self.visit_assignment(a, pos),
60-
Statement::SubCall(b, e) => self.visit_sub_call(b, pos, e),
60+
Statement::SubCall(sub_call) => self.visit_sub_call(sub_call, pos),
6161
Statement::BuiltInSubCall(b, e) => self.visit_built_in_sub_call(b, pos, e),
6262
Statement::IfBlock(i) => self.visit_if_block(i),
6363
Statement::SelectCase(s) => self.visit_select_case(s),
@@ -138,12 +138,8 @@ pub trait PostConversionLinter {
138138
Ok(())
139139
}
140140

141-
fn visit_sub_call(
142-
&mut self,
143-
_name: &CaseInsensitiveString,
144-
_pos: Position,
145-
args: &Expressions,
146-
) -> Result<(), LintErrorPos> {
141+
fn visit_sub_call(&mut self, sub_call: &SubCall, _pos: Position) -> Result<(), LintErrorPos> {
142+
let (_, args) = sub_call.into();
147143
self.visit_expressions(args)
148144
}
149145

rusty_linter/src/post_linter/user_defined_sub_linter.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::user_defined_function_linter::lint_call_args;
33
use crate::core::HasSubprograms;
44
use crate::core::{LintError, LintErrorPos};
55
use rusty_common::*;
6-
use rusty_parser::Expressions;
6+
use rusty_parser::SubCall;
77

88
pub struct UserDefinedSubLinter<'a, R> {
99
pub linter_context: &'a R,
@@ -13,12 +13,8 @@ impl<'a, R> PostConversionLinter for UserDefinedSubLinter<'a, R>
1313
where
1414
R: HasSubprograms,
1515
{
16-
fn visit_sub_call(
17-
&mut self,
18-
name: &CaseInsensitiveString,
19-
pos: Position,
20-
args: &Expressions,
21-
) -> Result<(), LintErrorPos> {
16+
fn visit_sub_call(&mut self, sub_call: &SubCall, pos: Position) -> Result<(), LintErrorPos> {
17+
let (name, args) = sub_call.into();
2218
match self.linter_context.subs().get(name) {
2319
Some(sub_signature_pos) => {
2420
lint_call_args(args, sub_signature_pos.element.param_types(), pos)

rusty_linter/src/tests/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn test_passing_array_parameter_with_parenthesis() {
104104
.build_list_rc(2, 9)
105105
))
106106
.at_rc(2, 5),
107-
GlobalStatement::Statement(Statement::SubCall(
107+
GlobalStatement::Statement(Statement::sub_call(
108108
"Menu".into(),
109109
vec![Expression::ArrayElement(
110110
"choice$".into(),

rusty_linter/src/tests/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn test_constant_definition_and_usage_in_sub_call_arg() {
133133
Expression::StringLiteral("hello".to_owned()).at_rc(2, 15)
134134
),)
135135
.at_rc(2, 5),
136-
GlobalStatement::Statement(Statement::SubCall(
136+
GlobalStatement::Statement(Statement::sub_call(
137137
"MySub".into(),
138138
vec![Expression::StringLiteral("hello".to_owned()).at_rc(3, 11)]
139139
))

rusty_parser/src/specific/built_ins/close.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mod tests {
6767
fn test_one_file_number_no_hash_no_leading_space() {
6868
let input = "CLOSE1";
6969
let statement = parse(input).demand_single_statement();
70-
assert_eq!(statement, Statement::SubCall("CLOSE1".into(), vec![]));
70+
assert_eq!(statement, Statement::sub_call("CLOSE1".into(), vec![]));
7171
}
7272

7373
#[test]

0 commit comments

Comments
 (0)