Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. use std::iter::Iterator;
  2. use std::collections::HashMap;
  3.  
  4. /// My attempt at abstracting it into a seperate struct
  5. #[derive(Clone, Debug, PartialEq)]
  6. pub struct StandartValuedHashMap<K, V> where
  7. K: std::clone::Clone + std::fmt::Debug + std::cmp::Eq + std::hash::Hash + std::cmp::PartialEq,
  8. V: std::clone::Clone + std::fmt::Debug + std::cmp::Eq + std::cmp::PartialEq {
  9. hashmap: HashMap<K, V>,
  10. pub generator: Box<Iterator<Item=V>>
  11. }
  12.  
  13. impl<K, V> StandartValuedHashMap<K, V> where
  14. K: std::clone::Clone + std::fmt::Debug + std::cmp::Eq + std::hash::Hash + std::cmp::PartialEq,
  15. V: std::clone::Clone + std::fmt::Debug + std::cmp::Eq + std::cmp::PartialEq {
  16. pub fn new(generator: Box<Iterator<Item=V>>) -> Self {
  17. let hashmap = HashMap::new();
  18. StandartValuedHashMap{hashmap, generator}
  19. }
  20.  
  21. pub fn get(&mut self, key: K) -> &V {
  22. let key2 = key.clone();
  23. if self.hashmap.get(&key).is_none() {
  24. self.hashmap.insert(key, self.generator.next().unwrap());
  25. }
  26. self.hashmap.get(&key2).unwrap()
  27. }
  28. }
  29.  
  30.  
  31.  
  32. // Original attempts
  33. pub struct Translator {
  34. // Tokenizer is an iterator for later use
  35. /// source Token stream
  36. source: Tokenizer,
  37. /// Number(!) of registers in target program
  38. ramend: usize,
  39. /// Jump marker identifier generator
  40. jmp_id_gen: Box<Iterator<Item=String>>,
  41. /// Instruction operand identifier generator
  42. inst_id_gen: Box<Iterator<Item=String>>,
  43. // Mapping the original identifiers to the translated ones
  44. global_scope: HashMap<String, String>
  45. }
  46.  
  47. impl Translator {
  48. pub fn new(source: Tokenizer) -> Self {
  49. let ramend = 0;
  50. let inst_id_gen = (0..0).map(|n| n.to_string());
  51. let inst_id_gen = Box::new(inst_id_gen);
  52. // all single ascii letters; lowercase alphabet followed by uppercase alphabet
  53. let jmp_id_gen = ASCII_LOWER.iter().chain(ASCII_UPPER.iter()).map(|c| c.to_string());
  54. let jmp_id_gen = Box::new(jmp_id_gen);
  55. let global_scope = HashMap::new();
  56. Translator{source, ramend, jmp_id_gen, inst_id_gen, global_scope}
  57. }
  58.  
  59. /// Set the instruction operator identifier generator to use a given ramend
  60. fn set_inst_id_gen(&mut self, ramend: usize) {
  61. // numbers from ramend-1 to 1
  62. let inst_id_gen = (0..ramend).map(|n| n.to_string()).rev();
  63. self.ramend = ramend;
  64. self.inst_id_gen = Box::new(inst_id_gen);
  65. }
  66.  
  67. // attempt 2
  68. fn instruction_operand(&mut self, index: String) -> String {
  69. let key = index.clone();
  70. self.global_scope.entry(index).or_insert_with(|| self.inst_id_gen.next().unwrap());
  71. self.global_scope.get(&key).unwrap().to_string()
  72. }
  73.  
  74. // attempt 1
  75. fn jump_marker(&mut self, index: String) -> String {
  76. match self.global_scope.get(&index) {
  77. Some(id) => id.to_string(),
  78. None => {
  79. self.global_scope.insert(
  80. index, self.jmp_id_gen.next().unwrap());
  81. self.global_scope.get(&index).unwrap().to_string()
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement