Skip to content

Commit a025cde

Browse files
committed
Add Global enum member to SubprogramName
1 parent be0cac1 commit a025cde

4 files changed

Lines changed: 58 additions & 36 deletions

File tree

rusty_basic/src/instruction_generator/dim.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rusty_common::*;
2+
use rusty_linter::SubprogramName;
23
use rusty_parser::*;
34
use rusty_variant::Variant;
45

@@ -45,13 +46,12 @@ impl InstructionGenerator {
4546

4647
impl InstructionGenerator {
4748
fn is_in_static_subprogram(&self) -> bool {
48-
match &self.current_subprogram {
49-
Some(subprogram_name) => {
50-
self.subprogram_info_repository
51-
.get_subprogram_info(subprogram_name)
52-
.is_static
53-
}
54-
_ => false,
49+
if self.current_subprogram == SubprogramName::Global {
50+
false
51+
} else {
52+
self.subprogram_info_repository
53+
.get_subprogram_info(&self.current_subprogram)
54+
.is_static
5555
}
5656
}
5757

rusty_basic/src/instruction_generator/main.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub struct InstructionGenerator {
264264
pub instructions: Vec<InstructionPos>,
265265
pub statement_addresses: Vec<usize>,
266266
pub subprogram_info_repository: SubprogramInfoRepository,
267-
pub current_subprogram: Option<SubprogramName>,
267+
pub current_subprogram: SubprogramName,
268268
pub linter_names: Names,
269269
}
270270

@@ -274,7 +274,7 @@ impl InstructionGenerator {
274274
instructions: vec![],
275275
statement_addresses: vec![],
276276
subprogram_info_repository,
277-
current_subprogram: None,
277+
current_subprogram: SubprogramName::Global,
278278
linter_names,
279279
}
280280
}
@@ -402,11 +402,16 @@ impl InstructionGenerator {
402402
}
403403

404404
fn mark_current_subprogram(&mut self, subprogram_name: SubprogramName, pos: Position) {
405+
debug_assert_ne!(
406+
subprogram_name,
407+
SubprogramName::Global,
408+
"should not mark global scope"
409+
);
405410
self.push(
406411
Instruction::Label(Self::format_subprogram_label(&subprogram_name)),
407412
pos,
408413
);
409-
self.current_subprogram = Some(subprogram_name);
414+
self.current_subprogram = subprogram_name;
410415
}
411416

412417
fn subprogram_body(&mut self, block: Statements, pos: Position) {
@@ -483,6 +488,9 @@ impl InstructionGenerator {
483488
s.push_str(sub_name.as_ref());
484489
s
485490
}
491+
SubprogramName::Global => {
492+
panic!("Should not generate label for global scope")
493+
}
486494
};
487495
BareName::new(s)
488496
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
use rusty_parser::{BareName, Name};
22

3+
use crate::core::NameScope;
4+
35
/// Holds the resolved name of a subprogram.
46
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
57
pub enum SubprogramName {
8+
/// The global scope.
9+
Global,
10+
611
/// The resolved qualified name of a function.
712
Function(Name),
813

914
/// The resolved name of a sub.
1015
Sub(BareName),
1116
}
17+
18+
impl From<&SubprogramName> for NameScope {
19+
fn from(subprogram_name: &SubprogramName) -> Self {
20+
match subprogram_name {
21+
SubprogramName::Global => Self::Global,
22+
SubprogramName::Function(_) => Self::Function,
23+
SubprogramName::Sub(_) => Self::Sub,
24+
}
25+
}
26+
}

rusty_linter/src/names/main.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ e.g. A = 3.14 (resolves as A! by the default rules), A$ = "hello", A% = 1
3131
5b. An extended variable cannot co-exist with other symbols of the same name
3232
*/
3333

34-
type Key = Option<SubprogramName>;
35-
3634
pub struct Names {
37-
data: HashMap<Key, NamesOneLevel>,
38-
current_key: Key,
35+
data: HashMap<SubprogramName, NamesOneLevel>,
36+
current_key: SubprogramName,
3937
}
4038

4139
/// Stores the data relevant to one level only (i.e. global symbols, or a FUNCTION, or a SUB).
@@ -47,12 +45,12 @@ struct NamesOneLevel(NamesInner, ImplicitVars);
4745

4846
impl Names {
4947
pub fn new() -> Self {
50-
let mut data: HashMap<Key, NamesOneLevel> = HashMap::new();
48+
let mut data: HashMap<SubprogramName, NamesOneLevel> = HashMap::new();
5149
// insert GLOBAL scope
52-
data.insert(None, NamesOneLevel::default());
50+
data.insert(SubprogramName::Global, NamesOneLevel::default());
5351
Self {
5452
data,
55-
current_key: None,
53+
current_key: SubprogramName::Global,
5654
}
5755
}
5856

@@ -79,8 +77,8 @@ impl Names {
7977
/// Returns the global names, but only if we are currently within a sub program.
8078
fn global_names(&self) -> Option<&NamesInner> {
8179
match &self.current_key {
82-
Some(_) => self.data.get(&None).map(|x| &x.0),
83-
None => None,
80+
SubprogramName::Global => None,
81+
_ => self.data.get(&SubprogramName::Global).map(|x| &x.0),
8482
}
8583
}
8684

@@ -160,39 +158,40 @@ impl Names {
160158

161159
pub fn is_in_function(&self, function_name: &BareName) -> bool {
162160
match &self.current_key {
163-
Some(SubprogramName::Function(f)) => f.as_bare_name() == function_name,
161+
SubprogramName::Function(f) => f.as_bare_name() == function_name,
164162
_ => false,
165163
}
166164
}
167165

168166
pub fn is_in_subprogram(&self) -> bool {
169-
self.current_key.is_some()
167+
self.current_key != SubprogramName::Global
170168
}
171169

172170
pub fn get_name_scope(&self) -> NameScope {
173-
match &self.current_key {
174-
Some(SubprogramName::Function(_)) => NameScope::Function,
175-
Some(SubprogramName::Sub(_)) => NameScope::Sub,
176-
None => NameScope::Global,
177-
}
171+
NameScope::from(&self.current_key)
178172
}
179173

180174
pub fn push(&mut self, subprogram_name: SubprogramName) {
181-
let key = Some(subprogram_name);
182175
debug_assert!(
183-
!self.data.contains_key(&key),
176+
!self.data.contains_key(&subprogram_name),
184177
"should not encounter same function/sub twice!"
185178
);
186-
self.current_key = key.clone();
187-
self.data.insert(key, NamesOneLevel::default());
179+
debug_assert_ne!(
180+
subprogram_name,
181+
SubprogramName::Global,
182+
"should not push global scope"
183+
);
184+
self.current_key = subprogram_name.clone();
185+
self.data.insert(subprogram_name, NamesOneLevel::default());
188186
}
189187

190188
pub fn pop(&mut self) {
191-
debug_assert!(
192-
self.current_key.is_some(),
189+
debug_assert_ne!(
190+
self.current_key,
191+
SubprogramName::Global,
193192
"should not pop context from the global level"
194193
);
195-
self.current_key = None;
194+
self.current_key = SubprogramName::Global;
196195
}
197196

198197
pub fn find_name_or_shared_in_parent(
@@ -208,7 +207,7 @@ impl Names {
208207

209208
pub fn get_resolved_variable_info(
210209
&self,
211-
subprogram_name: &Option<SubprogramName>,
210+
subprogram_name: &SubprogramName,
212211
name: &Name,
213212
) -> &VariableInfo {
214213
let one_level = self
@@ -218,11 +217,11 @@ impl Names {
218217
match one_level.0.get_variable_info_by_name(name) {
219218
Some(i) => i,
220219
None => {
221-
if subprogram_name.is_some() {
220+
if subprogram_name != &SubprogramName::Global {
222221
// try then parent level but only for SHARED
223222
let result = self
224223
.data
225-
.get(&None)
224+
.get(&SubprogramName::Global)
226225
.expect("Global name scope missing!")
227226
.0
228227
.get_variable_info_by_name(name)

0 commit comments

Comments
 (0)