Skip to content

Commit 791572b

Browse files
committed
refactor: Replace Statement::Const(name, value) with a struct
`Statement::Const(Constant)` This change should streamline the visitor pattern implementation in the linter.
1 parent 0c71834 commit 791572b

11 files changed

Lines changed: 46 additions & 32 deletions

File tree

rusty_basic/src/instruction_generator/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Visitor<StatementPos> for InstructionGenerator {
2626
Statement::Assignment(left_side, right_side) => {
2727
self.generate_assignment_instructions(left_side, right_side, pos)
2828
}
29-
Statement::Const(_, _) => {
29+
Statement::Const(_) => {
3030
// The CONST statement does not generate any instructions,
3131
// because the linter has replaced expressions that reference constants
3232
// with their actual value.

rusty_linter/src/converter/statement/const_rules.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ use crate::core::{LintError, LintErrorPos};
55
use rusty_common::*;
66
use rusty_parser::*;
77

8-
pub fn on_const(
9-
ctx: &mut Context,
10-
left_side: NamePos,
11-
right_side: ExpressionPos,
12-
) -> Result<Statement, LintErrorPos> {
8+
pub fn on_const(ctx: &mut Context, c: Constant) -> Result<Statement, LintErrorPos> {
9+
let (left_side, right_side) = c.into();
1310
const_cannot_clash_with_existing_names(ctx, &left_side)?;
1411
new_const(ctx, left_side, right_side)
1512
}
@@ -61,5 +58,5 @@ fn new_const(
6158
// However the `CONST` statement gets ignored anyway. It stored the resolved
6259
// value in the context, so all expressions that reference it
6360
// will use it.
64-
Ok(Statement::Const(const_name.at_pos(pos), right_side))
61+
Ok(Statement::constant(const_name.at_pos(pos), right_side))
6562
}

rusty_linter/src/converter/statement/main.rs

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

rusty_linter/src/post_linter/expression_reducer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ pub trait ExpressionReducer {
107107
Statement::Assignment(reduced_left, reduced_right)
108108
})
109109
}
110-
Statement::Const(left, right) => {
110+
Statement::Const(c) => {
111111
// The converter is smart enough to replace Expressions that reference
112112
// constants with their actual value, so the `CONST` statement isn't used
113113
// any further.
114-
Ok(Statement::Const(left, right))
114+
Ok(Statement::Const(c))
115115
}
116116
Statement::SubCall(b, e) => self
117117
.visit_sub_call(b, e)

rusty_linter/src/post_linter/post_conversion_linter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub trait PostConversionLinter {
7474
Statement::Resume(resume_option) => self.visit_resume(resume_option, pos),
7575
Statement::Return(opt_label) => self.visit_return(opt_label.as_ref(), pos),
7676
Statement::Exit(exit_object) => self.visit_exit(*exit_object),
77-
Statement::Const(_, _) | Statement::End | Statement::System => Ok(()),
77+
Statement::Const(_) | Statement::End | Statement::System => Ok(()),
7878
}
7979
}
8080

rusty_linter/src/pre_linter/const_rules.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ use crate::core::ConstValueResolver;
33
use crate::core::{LintError, LintErrorPos};
44
use crate::pre_linter::ConstantMap;
55
use rusty_common::{AtPos, Positioned};
6-
use rusty_parser::{BareName, ExpressionPos, Name, NamePos};
6+
use rusty_parser::Constant;
7+
use rusty_parser::{BareName, Name};
78

89
// calculate global constant values
9-
pub fn global_const(
10-
global_constants: &mut ConstantMap,
11-
name_pos: &NamePos,
12-
expression_pos: &ExpressionPos,
13-
) -> Result<(), LintErrorPos> {
10+
pub fn global_const(global_constants: &mut ConstantMap, c: &Constant) -> Result<(), LintErrorPos> {
11+
let (name_pos, expression_pos) = c.into();
1412
let Positioned { element: name, pos } = name_pos;
1513
let bare_name: &BareName = name.bare_name();
1614
(match global_constants.get(bare_name) {

rusty_linter/src/pre_linter/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Visitor<SubImplementation> for MainContext {
116116
impl Visitor<Statement> for MainContext {
117117
fn visit(&mut self, s: &Statement) -> VisitResult {
118118
match s {
119-
Statement::Const(name, expr) => global_const(&mut self.global_constants, name, expr),
119+
Statement::Const(c) => global_const(&mut self.global_constants, c),
120120
_ => Ok(()),
121121
}
122122
}

rusty_linter/src/tests/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn test_constant_definition_and_usage_in_print() {
106106
"#;
107107
assert_linter_ok_global_statements!(
108108
program,
109-
Statement::Const(
109+
Statement::constant(
110110
Name::from("X").at_rc(2, 11),
111111
Expression::StringLiteral("hello".to_owned()).at_rc(2, 15)
112112
),
@@ -128,7 +128,7 @@ fn test_constant_definition_and_usage_in_sub_call_arg() {
128128
assert_eq!(
129129
linter_ok(program),
130130
vec![
131-
GlobalStatement::Statement(Statement::Const(
131+
GlobalStatement::Statement(Statement::constant(
132132
Name::from("X").at_rc(2, 11),
133133
Expression::StringLiteral("hello".to_owned()).at_rc(2, 15)
134134
),)

rusty_parser/src/specific/core/constant.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn constant_p() -> impl Parser<RcStringView, Output = Statement> {
1313
.or_syntax_error("Expected: const name"),
1414
equal_sign(),
1515
expression_pos_p().or_syntax_error("Expected: const value"),
16-
|_, _, const_name, _, const_value_expr| Statement::Const(const_name, const_value_expr),
16+
|_, _, const_name, _, const_value_expr| Statement::constant(const_name, const_value_expr),
1717
)
1818
}
1919

@@ -34,11 +34,11 @@ mod tests {
3434
assert_eq!(
3535
program,
3636
vec![
37-
GlobalStatement::Statement(Statement::Const(
37+
GlobalStatement::Statement(Statement::constant(
3838
"X".as_name(2, 15),
3939
42.as_lit_expr(2, 19),
4040
)),
41-
GlobalStatement::Statement(Statement::Const(
41+
GlobalStatement::Statement(Statement::constant(
4242
"Y$".as_name(3, 15),
4343
"hello".as_lit_expr(3, 20),
4444
))
@@ -55,12 +55,10 @@ mod tests {
5555
let input = format!("CONST {} = {}", name, value);
5656
let statement = parse(&input).demand_single_statement();
5757
match statement {
58-
Statement::Const(
59-
Positioned { element: left, .. },
60-
Positioned { element: right, .. },
61-
) => {
62-
assert_eq!(left, Name::from(*name));
63-
assert_eq!(right, Expression::IntegerLiteral(*value));
58+
Statement::Const(c) => {
59+
let (left, right) = c.into();
60+
assert_eq!(left.element, Name::from(*name));
61+
assert_eq!(right.element, Expression::IntegerLiteral(*value));
6462
}
6563
_ => panic!("Expected constant"),
6664
}
@@ -75,7 +73,7 @@ mod tests {
7573
assert_eq!(
7674
program,
7775
vec![
78-
GlobalStatement::Statement(Statement::Const(
76+
GlobalStatement::Statement(Statement::constant(
7977
"ANSWER".as_name(1, 7),
8078
42.as_lit_expr(1, 16),
8179
))

rusty_parser/src/specific/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub use self::param_name::{ParamType, Parameter, ParameterPos, Parameters};
6666
pub use self::print::{Print, PrintArg};
6767
pub use self::qualified_name::QualifiedName;
6868
pub use self::statement::{
69-
CaseBlock, CaseExpression, ConditionalBlock, DimList, DoLoop, DoLoopConditionKind,
69+
CaseBlock, CaseExpression, ConditionalBlock, Constant, DimList, DoLoop, DoLoopConditionKind,
7070
DoLoopConditionPosition, ExitObject, ForLoop, IfBlock, OnErrorOption, ResumeOption, SelectCase,
7171
Statement, StatementPos, Statements,
7272
};

0 commit comments

Comments
 (0)