state.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use std::collections::{hash_map::Keys, HashMap};
  2. use crate::types::TypeId;
  3. #[derive(Debug, Clone)]
  4. pub struct Line {
  5. pub tokens: Vec<String>,
  6. pub orig: String,
  7. pub number: u32,
  8. }
  9. impl Line {
  10. pub fn new(tokens: Vec<String>, orig: String, number: u32) -> Self {
  11. Line {
  12. tokens,
  13. orig,
  14. number,
  15. }
  16. }
  17. }
  18. #[derive(Debug, Default, Clone)]
  19. pub struct Constants {
  20. pub table: Vec<TypeId>,
  21. pub map: HashMap<String, usize>,
  22. }
  23. impl Constants {
  24. pub fn new() -> Self {
  25. Constants {
  26. table: vec![],
  27. map: HashMap::new(),
  28. }
  29. }
  30. pub fn add(&mut self, variable: String, type_id: TypeId) {
  31. let idx = self.table.len();
  32. self.table.push(type_id);
  33. self.map.insert(variable, idx);
  34. }
  35. pub fn lookup(&self, variable: String) -> TypeId {
  36. if let Some(idx) = self.map.get(variable.as_str()) {
  37. return self.table[*idx];
  38. }
  39. panic!();
  40. }
  41. pub fn variables(&self) -> Keys<'_, String, usize> {
  42. self.map.keys()
  43. }
  44. }