Skip to content

Commit 866718d

Browse files
committed
refactor: Converted Name into a struct with private fields
`Name` used to be an enum with arms `Bare` and `Qualified`. Converting to `struct`, with the intention of eventually removing `BareName` and `QualifiedName` types.
1 parent fdc747e commit 866718d

14 files changed

Lines changed: 136 additions & 152 deletions

File tree

rusty_basic/src/instruction_generator/dim.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl InstructionGenerator {
9797
self.push(Instruction::AllocateBuiltIn(q), pos);
9898
self.push(
9999
Instruction::VarPathName(RootPath {
100-
name: Name::Qualified(bare_name, q),
100+
name: Name::qualified(bare_name, q),
101101
shared,
102102
}),
103103
pos,
@@ -108,7 +108,7 @@ impl InstructionGenerator {
108108
self.push(Instruction::AllocateFixedLengthString(len), pos);
109109
self.push(
110110
Instruction::VarPathName(RootPath {
111-
name: Name::Qualified(bare_name, TypeQualifier::DollarString),
111+
name: Name::qualified(bare_name, TypeQualifier::DollarString),
112112
shared,
113113
}),
114114
pos,
@@ -125,7 +125,7 @@ impl InstructionGenerator {
125125
);
126126
self.push(
127127
Instruction::VarPathName(RootPath {
128-
name: Name::Bare(bare_name),
128+
name: Name::bare(bare_name),
129129
shared,
130130
}),
131131
pos,

rusty_basic/src/instruction_generator/instruction_generator.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,16 @@ impl InstructionGenerator {
372372
body,
373373
..
374374
} = function_implementation;
375-
match function_name {
376-
Name::Bare(_) => panic!("Expected qualified function name"),
377-
Name::Qualified(bare_name, qualifier) => {
378-
let qualifier_copy = qualifier;
379-
let q_function_name = QualifiedName::new(bare_name, qualifier);
380-
self.mark_current_subprogram(SubprogramName::Function(q_function_name), pos);
381-
// set default value
382-
self.push(Instruction::AllocateBuiltIn(qualifier_copy), pos);
383-
self.subprogram_body(body, pos);
384-
}
385-
}
375+
376+
let qualifier = function_name
377+
.qualifier()
378+
.expect("Expected qualified function name");
379+
let bare_name = function_name.bare_name().clone();
380+
let q_function_name = QualifiedName::new(bare_name, qualifier);
381+
self.mark_current_subprogram(SubprogramName::Function(q_function_name), pos);
382+
// set default value
383+
self.push(Instruction::AllocateBuiltIn(qualifier), pos);
384+
self.subprogram_body(body, pos);
386385
}
387386

388387
fn visit_subs(&mut self, subs: Vec<Positioned<SubImplementation>>) {

rusty_basic/src/interpreter/variables.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ impl Variables {
4040
qualifier: TypeQualifier,
4141
value: Variant,
4242
) {
43-
self.insert(Name::Qualified(bare_name, qualifier), value);
43+
self.insert(Name::qualified(bare_name, qualifier), value);
4444
}
4545

4646
pub fn insert_user_defined(&mut self, bare_name: BareName, value: Variant) {
47-
self.insert(Name::Bare(bare_name), value);
47+
self.insert(Name::bare(bare_name), value);
4848
}
4949

5050
fn insert_unnamed(&mut self, value: Variant, arg_path: Option<Path>) {
5151
let dummy_name = format!("{}", self.map.len());
52-
let name = Name::Bare(BareName::new(dummy_name));
52+
let name = Name::bare(BareName::new(dummy_name));
5353
self.map
5454
.insert(name, RuntimeVariableInfo::new(value, arg_path));
5555
}
@@ -62,8 +62,8 @@ impl Variables {
6262
let (bare_name, param_type) = param_name.into();
6363
match param_type {
6464
ParamType::Bare => panic!("Unresolved param {:?}", bare_name),
65-
ParamType::BuiltIn(q, _) => Name::Qualified(bare_name, q),
66-
ParamType::UserDefined(_) => Name::Bare(bare_name),
65+
ParamType::BuiltIn(q, _) => Name::qualified(bare_name, q),
66+
ParamType::UserDefined(_) => Name::bare(bare_name),
6767
ParamType::Array(boxed_param_type) => {
6868
let dummy_param = Parameter::new(bare_name, *boxed_param_type);
6969
Self::param_to_name(dummy_param)
@@ -134,13 +134,13 @@ impl Variables {
134134

135135
pub fn get_built_in(&self, bare_name: &BareName, qualifier: TypeQualifier) -> Option<&Variant> {
136136
// TODO make a structure that allows to lookup by BareName and QualifiedName without the need to clone
137-
let temp = Name::Qualified(bare_name.clone(), qualifier);
137+
let temp = Name::qualified(bare_name.clone(), qualifier);
138138
self.get_by_name(&temp)
139139
}
140140

141141
pub fn get_user_defined(&self, bare_name: &BareName) -> Option<&Variant> {
142142
// TODO make a structure that allows to lookup by BareName and QualifiedName without the need to clone
143-
let temp = Name::Bare(bare_name.clone());
143+
let temp = Name::bare(bare_name.clone());
144144
self.get_by_name(&temp)
145145
}
146146

rusty_linter/src/converter/expr_rules/property.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn existing_property_element_type(
178178
property_name: Name,
179179
) -> Result<Expression, LintErrorPos> {
180180
let bare_name = property_name.into();
181-
let property_name = Name::Bare(bare_name);
181+
let property_name = Name::bare(bare_name);
182182
Ok(Expression::Property(
183183
Box::new(resolved_left_side),
184184
property_name,

rusty_linter/src/converter/expr_rules/qualify_name.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ pub fn qualify_name(expression_type: &ExpressionType, name: Name) -> Result<Name
1212
try_qualify(name, expr_q).map_err(|_| LintError::TypeMismatch)
1313
}
1414
None => {
15-
match name {
15+
if name.is_bare() {
16+
Ok(name)
17+
} else {
1618
// trying to use a qualifier on an ExpressionType that doesn't accept it
17-
Name::Qualified(_, _) => Err(LintError::TypeMismatch),
18-
_ => Ok(name),
19+
Err(LintError::TypeMismatch)
1920
}
2021
}
2122
}
@@ -24,10 +25,10 @@ pub fn qualify_name(expression_type: &ExpressionType, name: Name) -> Result<Name
2425
/// Tries to convert this name into a qualified name.
2526
/// Fails if the name is already qualified with a different qualifier.
2627
pub fn try_qualify(name: Name, qualifier: TypeQualifier) -> Result<Name, LintError> {
27-
match name {
28-
Name::Bare(bare_name) => Ok(Name::Qualified(bare_name, qualifier)),
29-
Name::Qualified(_, q) if q != qualifier => Err(LintError::DuplicateDefinition),
30-
_ => Ok(name),
28+
match name.qualifier() {
29+
Some(q) if q != qualifier => Err(LintError::DuplicateDefinition),
30+
Some(_) => Ok(name),
31+
None => Ok(Name::qualified(name.into(), qualifier)),
3132
}
3233
}
3334

@@ -60,29 +61,17 @@ pub fn try_built_in_function(n: &Name) -> Result<Option<BuiltInFunction>, LintEr
6061
| BuiltInFunction::Space
6162
| BuiltInFunction::UCase => {
6263
// ENVIRON$ must be qualified
63-
match n {
64-
Name::Bare(_) => Err(LintError::TypeMismatch),
65-
Name::Qualified(_, qualifier) => {
66-
if *qualifier == TypeQualifier::DollarString {
67-
Ok(Some(b))
68-
} else {
69-
Err(LintError::TypeMismatch)
70-
}
71-
}
64+
match n.qualifier() {
65+
Some(TypeQualifier::DollarString) => Ok(Some(b)),
66+
_ => Err(LintError::TypeMismatch),
7267
}
7368
}
7469
BuiltInFunction::Chr | BuiltInFunction::Str | BuiltInFunction::String => {
7570
// STR$ or otherwise it's undefined
76-
match n {
77-
// confirmed that even with DEFSTR A-Z it won't work as unqualified
78-
Name::Bare(_) => Ok(None),
79-
Name::Qualified(_, qualifier) => {
80-
if *qualifier == TypeQualifier::DollarString {
81-
Ok(Some(b))
82-
} else {
83-
Ok(None)
84-
}
85-
}
71+
// confirmed that even with DEFSTR A-Z it won't work as unqualified
72+
match n.qualifier() {
73+
Some(TypeQualifier::DollarString) => Ok(Some(b)),
74+
_ => Ok(None),
8675
}
8776
}
8877
},
@@ -94,8 +83,9 @@ fn demand_unqualified(
9483
built_in: BuiltInFunction,
9584
n: &Name,
9685
) -> Result<Option<BuiltInFunction>, LintError> {
97-
match n {
98-
Name::Bare(_) => Ok(Some(built_in)),
99-
_ => Err(LintError::TypeMismatch),
86+
if n.is_bare() {
87+
Ok(Some(built_in))
88+
} else {
89+
Err(LintError::TypeMismatch)
10090
}
10191
}

rusty_linter/src/core/type_resolver.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ impl IntoTypeQualifier for BareName {
2929

3030
impl IntoTypeQualifier for Name {
3131
fn qualify(&self, resolver: &impl TypeResolver) -> TypeQualifier {
32-
match self {
33-
Self::Bare(bare_name) => bare_name.qualify(resolver),
34-
Self::Qualified(_, qualifier) => *qualifier,
32+
match self.qualifier() {
33+
Some(qualifier) => qualifier,
34+
_ => self.bare_name().qualify(resolver),
3535
}
3636
}
3737
}
@@ -52,7 +52,7 @@ impl IntoQualified for BareName {
5252

5353
fn to_qualified(self, resolver: &impl TypeResolver) -> Self::Output {
5454
let q = self.qualify(resolver);
55-
Name::Qualified(self, q)
55+
Name::qualified(self, q)
5656
}
5757
}
5858

@@ -62,9 +62,11 @@ impl IntoQualified for Name {
6262
type Output = Self;
6363

6464
fn to_qualified(self, resolver: &impl TypeResolver) -> Self::Output {
65-
match self {
66-
Self::Bare(bare_name) => bare_name.to_qualified(resolver),
67-
_ => self,
65+
if self.is_bare() {
66+
let bare_name: BareName = self.into();
67+
bare_name.to_qualified(resolver)
68+
} else {
69+
self
6870
}
6971
}
7072
}

rusty_linter/src/names/names_inner.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ impl NamesInner {
4545
}
4646

4747
pub fn get_variable_info_by_name(&self, name: &Name) -> Option<&VariableInfo> {
48-
match name {
49-
// if it's bare, then it has to be extended
50-
Name::Bare(bare_name) => self.get_extended(bare_name),
48+
let bare_name = name.bare_name();
49+
match name.qualifier() {
5150
// if it's qualified, it can be either one (e.g. A$ or A AS STRING)
52-
Name::Qualified(bare_name, qualifier) => self
53-
.get_compact(bare_name, *qualifier)
51+
Some(qualifier) => self
52+
.get_compact(bare_name, qualifier)
5453
.or_else(|| self.get_extended(bare_name)),
54+
// if it's bare, then it has to be extended
55+
_ => self.get_extended(bare_name),
5556
}
5657
}
5758
}

rusty_linter/src/post_linter/user_defined_function_linter.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,17 @@ where
160160
pos: Position,
161161
args: &Expressions,
162162
) -> Result<(), LintErrorPos> {
163-
if let Name::Qualified(bare_name, qualifier) = name {
164-
match self.linter_context.functions().get(bare_name) {
165-
Some(function_signature_pos) => {
166-
if function_signature_pos.element != *qualifier {
167-
Err(LintError::TypeMismatch.at(function_signature_pos))
168-
} else {
169-
lint_call_args(args, function_signature_pos.element.param_types(), pos)
170-
}
163+
let qualifier = name.qualifier().expect("Unresolved function!");
164+
let bare_name = name.bare_name();
165+
match self.linter_context.functions().get(bare_name) {
166+
Some(function_signature_pos) => {
167+
if function_signature_pos.element != qualifier {
168+
Err(LintError::TypeMismatch.at(function_signature_pos))
169+
} else {
170+
lint_call_args(args, function_signature_pos.element.param_types(), pos)
171171
}
172-
None => self.handle_undefined_function(args),
173172
}
174-
} else {
175-
panic!("Unresolved function {:?}", name)
173+
None => self.handle_undefined_function(args),
176174
}
177175
}
178176

rusty_linter/src/pre_linter/constant_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use rusty_common::{AtPos, CaseInsensitiveString, Positioned};
4-
use rusty_parser::{BareName, Constant, Name};
4+
use rusty_parser::{BareName, Constant};
55
use rusty_variant::Variant;
66

77
use crate::core::*;
@@ -26,9 +26,9 @@ impl Visitor<Constant> for ConstantMap {
2626
_ => Ok(()),
2727
})
2828
.and_then(|_| self.resolve_const(expression_pos))
29-
.and_then(|v| match name {
30-
Name::Bare(_) => Ok(v),
31-
Name::Qualified(_, qualifier) => v.cast(*qualifier).map_err(|e| e.at(expression_pos)),
29+
.and_then(|v| match name.qualifier() {
30+
Some(qualifier) => v.cast(qualifier).map_err(|e| e.at(expression_pos)),
31+
_ => Ok(v),
3232
})
3333
.map(|casted| {
3434
self.0.insert(bare_name.clone(), casted);

rusty_parser/src/specific/core/expression.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,9 @@ impl HasExpressionType for Expression {
346346
expression_type.clone()
347347
}
348348
}
349-
Self::FunctionCall(Name::Qualified(_, qualifier), _) => {
350-
ExpressionType::BuiltIn(*qualifier)
351-
}
349+
Self::FunctionCall(name, _) => name.expression_type(),
352350
Self::BuiltInFunctionCall(f, _) => ExpressionType::BuiltIn(f.into()),
353351
Self::UnaryExpression(_, c) | Self::Parenthesis(c) => c.expression_type(),
354-
Self::FunctionCall(Name::Bare(_), _) => ExpressionType::Unresolved,
355352
}
356353
}
357354
}
@@ -806,7 +803,7 @@ mod variable {
806803
.map(|token| token.text)
807804
.collect();
808805
let mut result = Expression::Variable(
809-
Name::Bare(BareName::new(property_names.pop_front().unwrap())),
806+
Name::bare(BareName::new(property_names.pop_front().unwrap())),
810807
VariableInfo::unresolved(),
811808
);
812809
while let Some(property_name) = property_names.pop_front() {

0 commit comments

Comments
 (0)