ast.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. pub use crate::lexer::{FileRef, Located, Span};
  2. use std::fmt;
  3. pub type StrRef = string_interner::DefaultSymbol;
  4. pub type Name = Located<StrRef>;
  5. pub struct ASTArena {
  6. files: Vec<String>,
  7. strings: string_interner::StringInterner,
  8. exprs: Vec<Expr>,
  9. }
  10. impl Default for ASTArena {
  11. fn default() -> Self {
  12. Self::new()
  13. }
  14. }
  15. impl ASTArena {
  16. pub fn new() -> ASTArena {
  17. ASTArena {
  18. files: Vec::new(),
  19. strings: string_interner::StringInterner::new(),
  20. exprs: vec![Expr::Nil],
  21. }
  22. }
  23. pub fn expr_nil(&self) -> ExprId {
  24. ExprId { idx: 0 }
  25. }
  26. pub fn add_expr(&mut self, e: Expr) -> ExprId {
  27. let idx = self.exprs.len();
  28. self.exprs.push(e);
  29. ExprId { idx }
  30. }
  31. pub fn add_string(&mut self, s: &str) -> string_interner::DefaultSymbol {
  32. self.strings.get_or_intern(s)
  33. }
  34. pub fn show_stmt(&self, stmt: &Stmt, f: &mut fmt::Formatter) -> fmt::Result {
  35. match stmt {
  36. Stmt::Puts(expr) => {
  37. write!(f, "Puts ")?;
  38. self.show_expr(&self[expr.item], f, 0)
  39. }
  40. Stmt::Fix(name) => writeln!(f, "Fix {}", &self[name.item]),
  41. Stmt::Assn(fixed, name, expr) => {
  42. write!(
  43. f,
  44. "Assn {} {} ",
  45. if *fixed { "fixed" } else { "" },
  46. &self[name.item]
  47. )?;
  48. self.show_expr(&self[expr.item], f, 0)
  49. }
  50. Stmt::LitAssn(fixed, name, strs) => {
  51. write!(
  52. f,
  53. "LitAssn {} {}, [ ",
  54. if *fixed { "fixed" } else { "" },
  55. &self[name.item],
  56. )?;
  57. for str in strs.iter() {
  58. let s = &self[str.item];
  59. write!(f, " {} ", s)?;
  60. }
  61. writeln!(f, "]")
  62. }
  63. }
  64. }
  65. pub fn add_file(&mut self, file: String) -> FileRef {
  66. let idx = self.files.len();
  67. self.files.push(file);
  68. FileRef { idx }
  69. }
  70. pub fn get_file(&self, file: FileRef) -> &str {
  71. &self.files[file.idx]
  72. }
  73. pub fn get_line(&self, file: FileRef, span: Span) -> String {
  74. let mut line_number = 1;
  75. let mut start_of_line = 0;
  76. let mut end_of_line = None;
  77. let src = &self.files[file.idx];
  78. for (i, ch) in src.char_indices() {
  79. if ch == '\n' {
  80. if i < span.start as usize {
  81. line_number += 1;
  82. start_of_line = i;
  83. }
  84. if i >= span.end as usize && end_of_line.is_none() {
  85. end_of_line = Some(i);
  86. }
  87. }
  88. }
  89. let end_of_line = end_of_line.unwrap_or(src.len());
  90. let mut result = format!("{:3} |", line_number);
  91. result.push_str(&src[start_of_line..end_of_line]);
  92. result.push_str("\n ");
  93. for _ in start_of_line..(span.start as usize) {
  94. result.push(' ');
  95. }
  96. for _ in span.start..span.end {
  97. result.push('^');
  98. }
  99. result
  100. }
  101. fn indent(&self, f: &mut fmt::Formatter, depth: usize) -> fmt::Result {
  102. for _ in 0..depth {
  103. write!(f, " ")?;
  104. }
  105. Ok(())
  106. }
  107. fn show_pat(&self, pat: &Pat, f: &mut fmt::Formatter) -> fmt::Result {
  108. match pat {
  109. Pat::Wildcard => write!(f, "_"),
  110. Pat::Var(n) => write!(f, "{}", &self[n.item]),
  111. Pat::Lit(Literal::Atom(n)) => write!(f, "{}", &self[n.item]),
  112. Pat::Lit(lit) => write!(f, "{:?}", lit),
  113. Pat::Tup(tup) => {
  114. write!(f, "Tup( ")?;
  115. for t in tup {
  116. self.show_pat(t, f)?;
  117. write!(f, " ")?;
  118. }
  119. write!(f, ")")
  120. }
  121. }
  122. }
  123. fn show_expr(&self, expr: &Expr, f: &mut fmt::Formatter, depth: usize) -> fmt::Result {
  124. match expr {
  125. Expr::Nil => writeln!(f, "Nil"),
  126. Expr::Var(v) => writeln!(f, "Var({})", &self[v.item]),
  127. Expr::Lit(Literal::Atom(n)) => writeln!(f, "Lit(Atom({}))", &self[n.item]),
  128. Expr::Lit(lit) => writeln!(f, "{:?}", lit),
  129. Expr::Range(from, to) => {
  130. writeln!(f, "Range(")?;
  131. self.indent(f, depth + 2)?;
  132. self.show_expr(&self[*from], f, depth + 2)?;
  133. self.indent(f, depth + 2)?;
  134. self.show_expr(&self[*to], f, depth + 2)?;
  135. self.indent(f, depth)?;
  136. writeln!(f, ")")
  137. }
  138. Expr::Ap(func, args) => {
  139. writeln!(f, "Ap(")?;
  140. self.indent(f, depth + 2)?;
  141. self.show_expr(&self[*func], f, depth + 2)?;
  142. for arg in args {
  143. self.indent(f, depth + 2)?;
  144. self.show_expr(&self[*arg], f, depth + 2)?;
  145. }
  146. self.indent(f, depth)?;
  147. writeln!(f, ")")
  148. }
  149. Expr::Tup(expr) => {
  150. writeln!(f, "Tup(")?;
  151. for e in expr {
  152. self.indent(f, depth + 2)?;
  153. self.show_expr(&self[*e], f, depth + 2)?;
  154. }
  155. self.indent(f, depth)?;
  156. writeln!(f, ")")
  157. }
  158. Expr::Cat(expr) => {
  159. writeln!(f, "Cat(")?;
  160. for e in expr {
  161. self.indent(f, depth + 2)?;
  162. self.show_expr(&self[*e], f, depth + 2)?;
  163. }
  164. self.indent(f, depth)?;
  165. writeln!(f, ")")
  166. }
  167. Expr::Chc(expr) => {
  168. writeln!(f, "Chc(")?;
  169. for e in expr {
  170. if let Some(s) = e.weight {
  171. self.indent(f, depth + 2)?;
  172. writeln!(f, "{}:", s)?;
  173. self.indent(f, depth + 4)?;
  174. self.show_expr(&self[e.value], f, depth + 4)?;
  175. } else {
  176. self.indent(f, depth + 2)?;
  177. self.show_expr(&self[e.value], f, depth + 2)?;
  178. }
  179. }
  180. self.indent(f, depth)?;
  181. writeln!(f, ")")
  182. }
  183. Expr::Let(fixed, name, expr, body) => {
  184. writeln!(
  185. f,
  186. "Let({}{}",
  187. if *fixed { "fixed " } else { "" },
  188. &self[name.item]
  189. )?;
  190. self.indent(f, depth + 2)?;
  191. self.show_expr(&self[*expr], f, depth + 2)?;
  192. self.indent(f, depth + 2)?;
  193. self.show_expr(&self[*body], f, depth + 2)?;
  194. self.indent(f, depth)?;
  195. writeln!(f, ")")
  196. }
  197. Expr::Fun(cases) => {
  198. writeln!(f, "Fun(")?;
  199. for case in cases {
  200. self.indent(f, depth + 2)?;
  201. for pat in case.pats.iter() {
  202. self.show_pat(pat, f)?;
  203. writeln!(f, ", ")?;
  204. }
  205. writeln!(f, " =>")?;
  206. self.indent(f, depth + 4)?;
  207. self.show_expr(&self[case.expr], f, depth + 4)?;
  208. }
  209. self.indent(f, depth)?;
  210. writeln!(f, ")")
  211. }
  212. Expr::Case(expr, cases) => {
  213. writeln!(f, "Case(")?;
  214. self.indent(f, depth)?;
  215. self.show_expr(&self[*expr], f, depth)?;
  216. for case in cases {
  217. self.indent(f, depth + 2)?;
  218. self.show_pat(&case.pats[0], f)?;
  219. writeln!(f, " =>")?;
  220. self.indent(f, depth + 4)?;
  221. self.show_expr(&self[case.expr], f, depth + 4)?;
  222. }
  223. self.indent(f, depth)?;
  224. writeln!(f, ")")
  225. }
  226. }
  227. }
  228. }
  229. impl std::ops::Index<string_interner::DefaultSymbol> for ASTArena {
  230. type Output = str;
  231. fn index(&self, sf: string_interner::DefaultSymbol) -> &str {
  232. self.strings.resolve(sf).unwrap()
  233. }
  234. }
  235. impl std::ops::Index<ExprRef> for ASTArena {
  236. type Output = Expr;
  237. fn index(&self, rf: ExprRef) -> &Self::Output {
  238. &self.exprs[rf.item.idx]
  239. }
  240. }
  241. impl std::ops::Index<ExprId> for ASTArena {
  242. type Output = Expr;
  243. fn index(&self, rf: ExprId) -> &Self::Output {
  244. &self.exprs[rf.idx]
  245. }
  246. }
  247. /// A `Printable` struct is a bundle of another value and an
  248. /// `ASTArena`, which allows us to fetch the various indices and
  249. /// dereference the interned strings.
  250. pub struct Printable<'a, T> {
  251. arena: &'a ASTArena,
  252. value: &'a T,
  253. }
  254. impl<'a> std::fmt::Debug for Printable<'a, Stmt> {
  255. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  256. self.arena.show_stmt(self.value, f)
  257. }
  258. }
  259. impl<'a> std::fmt::Debug for Printable<'a, Expr> {
  260. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  261. self.arena.show_expr(self.value, f, 0)
  262. }
  263. }
  264. /// A top-level Matzo statement
  265. #[derive(Debug, Clone)]
  266. pub enum Stmt {
  267. /// evaluate and print the value of an expression
  268. Puts(ExprRef),
  269. /// replace a named item with the forced version of that item
  270. Fix(Name),
  271. /// assign a value to a name which may or may not be forced
  272. Assn(bool, Name, ExprRef),
  273. /// assign one of a set of strings to a name, which may or may not
  274. /// be forced
  275. LitAssn(bool, Name, Vec<Name>),
  276. }
  277. impl Stmt {
  278. pub fn show<'a>(&'a self, ast: &'a ASTArena) -> Printable<'a, Stmt> {
  279. Printable {
  280. arena: ast,
  281. value: self,
  282. }
  283. }
  284. }
  285. /// A Matzo expression
  286. #[derive(Debug, Clone)]
  287. pub enum Expr {
  288. Var(Name),
  289. Cat(Vec<ExprRef>),
  290. Chc(Vec<Choice>),
  291. Lit(Literal),
  292. Ap(ExprRef, Vec<ExprRef>),
  293. Tup(Vec<ExprRef>),
  294. Let(bool, Name, ExprRef, ExprRef),
  295. Fun(Vec<Case>),
  296. Range(ExprRef, ExprRef),
  297. Case(ExprRef, Vec<Case>),
  298. Nil,
  299. }
  300. pub type ExprRef = Located<ExprId>;
  301. #[derive(Debug, Copy, Clone, PartialEq, Eq)]
  302. pub struct ExprId {
  303. idx: usize,
  304. }
  305. impl ExprId {
  306. pub fn nil(&self) -> bool {
  307. self.idx == 0
  308. }
  309. }
  310. /// A single case in an anonymous function or `case` statement
  311. #[derive(Debug, Clone)]
  312. pub struct Case {
  313. pub pats: Vec<Pat>,
  314. pub expr: ExprRef,
  315. }
  316. /// A pattern, e.g. in an anonymous function or `case` statement
  317. #[derive(Debug, Clone)]
  318. pub enum Pat {
  319. Var(Name),
  320. Wildcard,
  321. Lit(Literal),
  322. Tup(Vec<Pat>),
  323. }
  324. /// A single element in a choice, with an optional weight (which
  325. /// defaults to 1) and a value
  326. #[derive(Debug, Clone)]
  327. pub struct Choice {
  328. pub weight: Option<i64>,
  329. pub value: ExprRef,
  330. }
  331. impl Choice {
  332. /// Fetch a weight from a `Choice`, defaulting to 1
  333. pub fn weight(&self) -> i64 {
  334. self.weight.unwrap_or(1)
  335. }
  336. }
  337. /// An atomic literal: a string, a number, or an atom
  338. #[derive(Debug, Clone)]
  339. pub enum Literal {
  340. Str(String),
  341. Atom(Name),
  342. Num(i64),
  343. }
  344. impl PartialEq for Literal {
  345. fn eq(&self, other: &Literal) -> bool {
  346. match (self, other) {
  347. (Literal::Str(s1), Literal::Str(s2)) => s1 == s2,
  348. (Literal::Atom(a1), Literal::Atom(a2)) => a1.item == a2.item,
  349. (Literal::Num(n1), Literal::Num(n2)) => n1 == n2,
  350. (_, _) => false,
  351. }
  352. }
  353. }