-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Lbl RE | ||
Menu("MY TERRIBLE GAME","PLAY",PL,"EXIT",0,"PLEASE LEAVE",0,"RESTART",RE | ||
Lbl PL | ||
Goto 0 | ||
Lbl 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use crate::parse::commands::{control_flow::Menu, ControlFlow}; | ||
use crate::parse::{ | ||
commands::{Command, LabelName}, | ||
Program, | ||
}; | ||
|
||
impl Program { | ||
/// Compute a mapping from label names to the index of the line where the label was defined. | ||
pub fn label_declarations(&self) -> BTreeMap<LabelName, usize> { | ||
let mut declarations = BTreeMap::new(); | ||
|
||
for (idx, line) in self.lines.iter().enumerate() { | ||
if let Command::ControlFlow(ControlFlow::Lbl(name)) = line { | ||
declarations.insert(*name, idx); | ||
} | ||
} | ||
|
||
declarations | ||
} | ||
|
||
/// Compute a mapping from label names to label usages (namely, `Goto `, `Menu(`) | ||
/// | ||
/// If a `Menu(` references the same label name more than once, the line will appear in the | ||
/// usages that many times. | ||
pub fn label_usages(&self) -> BTreeMap<LabelName, Vec<usize>> { | ||
let mut usages: BTreeMap<LabelName, Vec<usize>> = BTreeMap::new(); | ||
|
||
for (idx, line) in self.lines.iter().enumerate() { | ||
match line { | ||
Command::ControlFlow(ControlFlow::Goto(label)) => { | ||
usages.entry(*label).or_default().push(idx); | ||
} | ||
|
||
Command::ControlFlow(ControlFlow::Menu(Menu { option_labels, .. })) => { | ||
for label in option_labels { | ||
usages.entry(*label).or_default().push(idx); | ||
} | ||
} | ||
|
||
_ => {} | ||
} | ||
} | ||
|
||
usages | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::label_name; | ||
use test_files::{load_test_data, test_version}; | ||
use titokens::Tokenizer; | ||
|
||
fn program() -> Program { | ||
let mut tokens = load_test_data("/snippets/analysis/labels.txt"); | ||
let tokenizer = Tokenizer::new(test_version(), "en"); | ||
|
||
Program::from_tokens(&mut tokens, &tokenizer) | ||
} | ||
|
||
#[test] | ||
fn label_usages() { | ||
let test_program = program(); | ||
|
||
let mut expected = BTreeMap::new(); | ||
expected.insert(label_name!('R' 'E'), vec![1]); | ||
expected.insert(label_name!('P' 'L'), vec![1]); | ||
expected.insert(label_name!('0'), vec![1, 1, 3]); | ||
|
||
assert_eq!(test_program.label_usages(), expected) | ||
} | ||
|
||
#[test] | ||
fn label_declarations() { | ||
let test_program = program(); | ||
|
||
let mut expected = BTreeMap::new(); | ||
expected.insert(label_name!('R' 'E'), 0usize); | ||
expected.insert(label_name!('P' 'L'), 2); | ||
expected.insert(label_name!('0'), 4); | ||
|
||
assert_eq!(test_program.label_declarations(), expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod labels; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod control_flow; | ||
pub mod control_flow; | ||
mod delvar_chain; | ||
mod generic; | ||
mod prgm; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters