ast.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. pub type Name = string_interner::DefaultSymbol;
  2. pub struct ASTArena {
  3. strings: string_interner::StringInterner,
  4. }
  5. impl ASTArena {
  6. pub fn new() -> ASTArena {
  7. ASTArena {
  8. strings: string_interner::StringInterner::new(),
  9. }
  10. }
  11. pub fn add_string(&mut self, s: &str) -> Name {
  12. self.strings.get_or_intern(s)
  13. }
  14. }
  15. impl std::ops::Index<string_interner::DefaultSymbol> for ASTArena {
  16. type Output = str;
  17. fn index(&self, sf: string_interner::DefaultSymbol) -> &str {
  18. self.strings.resolve(sf).unwrap()
  19. }
  20. }
  21. #[derive(Debug, Clone)]
  22. pub enum Stmt {
  23. Puts(Expr),
  24. Fix(Name),
  25. Assn(Name, Expr),
  26. LitAssn(Name, Vec<String>),
  27. }
  28. #[derive(Debug, Clone)]
  29. pub enum Expr {
  30. Var(Name),
  31. Cat(Vec<Expr>),
  32. Chc(Vec<Choice>),
  33. Rep(i64, Box<Expr>),
  34. Lit(Literal),
  35. Ap(Box<Expr>, Box<Expr>),
  36. Tup(Vec<Expr>),
  37. Let(Name, Box<Expr>, Box<Expr>),
  38. Fun(Vec<Case>),
  39. Case(Box<Expr>, Vec<Case>),
  40. Range(Box<Expr>, Box<Expr>),
  41. }
  42. #[derive(Debug, Clone)]
  43. pub struct Case {
  44. pub pat: Pat,
  45. pub expr: Expr,
  46. }
  47. #[derive(Debug, Clone)]
  48. pub enum Pat {
  49. Var(Name),
  50. Lit(Literal),
  51. Tup(Vec<Pat>),
  52. }
  53. #[derive(Debug, Clone)]
  54. pub struct Choice {
  55. pub weight: Option<i64>,
  56. pub value: Expr,
  57. }
  58. impl Choice {
  59. pub fn weight(&self) -> i64 {
  60. self.weight.unwrap_or(1)
  61. }
  62. }
  63. #[derive(Debug, Clone)]
  64. pub enum Literal {
  65. Str(String),
  66. Atom(Name),
  67. Num(i64),
  68. }