Skip to content

Commit

Permalink
Add a second compilation pass to resolve base opcode references
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed Apr 15, 2024
1 parent e782b27 commit 4a24663
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl TranslationTable {
.filter(|r| r.is_direction(direction))
.collect();

for rule in rules {
for rule in &rules {
match rule {
Rule::Undefined { dots } => {
undefined = Some(Translation::new("".into(), dots_to_unicode(&dots), 0));
Expand Down Expand Up @@ -117,21 +117,10 @@ impl TranslationTable {
character, dots, ..
} => {
character_definitions.insert(
character,
*character,
Translation::new(character.to_string(), dots_to_unicode(&dots), 1),
);
}
Rule::Base { derived, base, .. } => {
if let Some(translation) = character_definitions.get(&base) {
character_definitions.insert(derived, Translation { from: derived.to_string(), ..translation.clone() });
} else {
// FIXME: return an error here instead of logging
eprintln!(
"Character in base rule not defined: derived: {}, base: {}",
derived, base
);
}
}
Rule::Comp6 {
chars,
dots: Braille::Explicit(dots),
Expand Down Expand Up @@ -232,6 +221,32 @@ impl TranslationTable {
_ => (),
}
}

// use a second pass through the rules to resolve the base opcodes and
// `=` arguments in rules
for rule in rules {
match rule {
Rule::Base { derived, base, .. } => {
if let Some(translation) = character_definitions.get(&base) {
character_definitions.insert(
derived,
Translation {
from: derived.to_string(),
..translation.clone()
},
);
} else {
// FIXME: return an error here instead of logging
eprintln!(
"Character in base rule not defined: derived: {}, base: {}, direction: {:?}",
derived, base, direction
);
}
}
_ => (),
}
}

TranslationTable {
undefined,
direction,
Expand Down

0 comments on commit 4a24663

Please sign in to comment.