Browse Source

switch to using custom AST display

Getty Ritter 2 years ago
parent
commit
2ee63c8829
7 changed files with 266 additions and 34 deletions
  1. 6 4
      build.rs
  2. 80 4
      src/ast.rs
  3. 10 6
      tests/assn.parsed
  4. 5 1
      tests/atom_lit.parsed
  5. 130 13
      tests/exprs.parsed
  6. 5 1
      tests/num_lit.parsed
  7. 30 5
      tests/str_lit.parsed

+ 6 - 4
build.rs

@@ -33,12 +33,14 @@ fn test_%PREFIX%() {
   let mut ast = crate::ast::ASTArena::new();
   let source = include_str!(\"%ROOT%/tests/%PREFIX%.matzo\");
   let lexer = lexer::tokens(source);
-  let ast = grammar::StmtsParser::new().parse(&mut ast, lexer);
-  assert!(ast.is_ok());
-  let ast = ast.unwrap();
+  let stmts = grammar::StmtsParser::new().parse(&mut ast, lexer);
+  assert!(stmts.is_ok());
+  let stmts = stmts.unwrap();
 
   let mut buf = Vec::new();
-  writeln!(buf, \"{:#?}\", ast).unwrap();
+  for s in stmts {
+    writeln!(buf, \"{:?}\", s.show(&ast)).unwrap();
+  }
   assert_eq(
     std::str::from_utf8(&buf).unwrap().trim(),
     include_str!(\"%ROOT%/tests/%PREFIX%.parsed\").trim(),

+ 80 - 4
src/ast.rs

@@ -30,12 +30,16 @@ impl ASTArena {
 
     pub fn show_stmt(&self, stmt: &Stmt, f: &mut fmt::Formatter) -> fmt::Result {
         match stmt {
-            Stmt::Puts(expr) =>
-                writeln!(f, "{:?}", expr),
+            Stmt::Puts(expr) => {
+                write!(f, "Puts ")?;
+                self.show_expr(&expr, f, 0)
+            }
             Stmt::Fix(name) =>
                 writeln!(f, "Fix({})", &self[*name]),
-            Stmt::Assn(name, expr) =>
-                writeln!(f, "Assn(\n  {},\n  {:?}\n)", &self[*name], expr),
+            Stmt::Assn(name, expr) => {
+                write!(f, "Assn {} ", &self[*name])?;
+                self.show_expr(&expr, f, 0)
+            }
             Stmt::LitAssn(name, strs) => {
                 write!(f, "LitAssn({}, [ ", &self[*name])?;
                 for str in strs.iter() {
@@ -45,6 +49,78 @@ impl ASTArena {
             }
         }
     }
+
+    fn indent(&self, f: &mut fmt::Formatter, depth: usize) -> fmt::Result {
+        for _ in 0..depth {
+            write!(f, " ")?;
+        }
+        Ok(())
+    }
+
+    fn show_expr(&self, expr: &Expr, f: &mut fmt::Formatter, depth: usize) -> fmt::Result {
+        match expr {
+            Expr::Var(v) => writeln!(f, "Var({})", &self[*v]),
+            Expr::Lit(Literal::Atom(n)) => writeln!(f, "Lit(Atom({}))", &self[*n]),
+            Expr::Lit(lit) => writeln!(f, "{:?}", lit),
+            Expr::Range(from, to) => {
+                writeln!(f, "Range(")?;
+                self.indent(f, depth + 2)?;
+                self.show_expr(from, f, depth + 2)?;
+                self.indent(f, depth + 2)?;
+                self.show_expr(to, f, depth + 2)?;
+                self.indent(f, depth)?;
+                writeln!(f, ")")
+            }
+
+            Expr::Ap(func, arg) => {
+                writeln!(f, "Ap(")?;
+                self.indent(f, depth + 2)?;
+                self.show_expr(func, f, depth + 2)?;
+                self.indent(f, depth + 2)?;
+                self.show_expr(arg, f, depth + 2)?;
+                self.indent(f, depth)?;
+                writeln!(f, ")")
+            }
+
+            Expr::Tup(expr) => {
+                writeln!(f, "Tup(")?;
+                for e in expr {
+                    self.indent(f, depth + 2)?;
+                    self.show_expr(e, f, depth + 2)?;
+                }
+                self.indent(f, depth)?;
+                writeln!(f, ")")
+            }
+
+            Expr::Cat(expr) => {
+                writeln!(f, "Cat(")?;
+                for e in expr {
+                    self.indent(f, depth + 2)?;
+                    self.show_expr(e, f, depth + 2)?;
+                }
+                self.indent(f, depth)?;
+                writeln!(f, ")")
+            }
+
+            Expr::Chc(expr) => {
+                writeln!(f, "Chc(")?;
+                for e in expr {
+                    if let Some(s) = e.weight {
+                        self.indent(f, depth + 2)?;
+                        writeln!(f, "{}:", s)?;
+                        self.indent(f, depth + 4)?;
+                        self.show_expr(&e.value, f, depth + 4)?;
+                    } else {
+                        self.indent(f, depth + 2)?;
+                        self.show_expr(&e.value, f, depth + 2)?;
+                    }
+                }
+                self.indent(f, depth)?;
+                writeln!(f, ")")
+            }
+            _ => writeln!(f, "[???]"),
+        }
+    }
 }
 
 impl std::ops::Index<string_interner::DefaultSymbol> for ASTArena {

+ 10 - 6
tests/assn.parsed

@@ -1,10 +1,14 @@
-Assn(
-  x,
-  Chc([Choice { weight: None, value: Cat([Lit(Num(5))]) }]))
+Assn x Chc(
+  Cat(
+    Num(5)
+  )
+)
 
-Assn(
-  y,
-  Chc([Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 3 }))]) }]))
+Assn y Chc(
+  Cat(
+    Lit(Atom(This))
+  )
+)
 
 LitAssn(a, [  a  b  c  d ]
 

+ 5 - 1
tests/atom_lit.parsed

@@ -1,2 +1,6 @@
-Chc([Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 1 }))]) }])
+Puts Chc(
+  Cat(
+    Lit(Atom(Foo))
+  )
+)
 

+ 130 - 13
tests/exprs.parsed

@@ -1,26 +1,143 @@
-Chc([Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 1 })), Lit(Atom(SymbolU32 { value: 2 })), Lit(Atom(SymbolU32 { value: 3 }))]) }])
+Puts Chc(
+  Cat(
+    Lit(Atom(This))
+    Lit(Atom(That))
+    Lit(Atom(The-Other))
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 1 }))]) }, Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 2 }))]) }, Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 3 }))]) }])
+Puts Chc(
+  Cat(
+    Lit(Atom(This))
+  )
+  Cat(
+    Lit(Atom(That))
+  )
+  Cat(
+    Lit(Atom(The-Other))
+  )
+)
 
-Chc([Choice { weight: Some(5), value: Cat([Lit(Atom(SymbolU32 { value: 1 }))]) }, Choice { weight: None, value: Cat([Lit(Atom(SymbolU32 { value: 2 }))]) }])
+Puts Chc(
+  5:
+    Cat(
+      Lit(Atom(This))
+    )
+  Cat(
+    Lit(Atom(That))
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Ap(Ap(Var(SymbolU32 { value: 4 }), Var(SymbolU32 { value: 5 })), Var(SymbolU32 { value: 6 }))]) }])
+Puts Chc(
+  Cat(
+    Ap(
+      Ap(
+        Var(foo)
+        Var(bar)
+      )
+      Var(baz)
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Tup([Chc([Choice { weight: None, value: Cat([]) }])])]) }])
+Puts Chc(
+  Cat(
+    Tup(
+      Chc(
+        Cat(
+        )
+      )
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Tup([Chc([Choice { weight: None, value: Cat([Lit(Num(1))]) }])])]) }])
+Puts Chc(
+  Cat(
+    Tup(
+      Chc(
+        Cat(
+          Num(1)
+        )
+      )
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Tup([Chc([Choice { weight: None, value: Cat([Lit(Num(1))]) }]), Chc([Choice { weight: None, value: Cat([Lit(Num(2))]) }])])]) }])
+Puts Chc(
+  Cat(
+    Tup(
+      Chc(
+        Cat(
+          Num(1)
+        )
+      )
+      Chc(
+        Cat(
+          Num(2)
+        )
+      )
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Tup([Chc([Choice { weight: None, value: Cat([Lit(Num(1))]) }]), Chc([Choice { weight: None, value: Cat([Lit(Num(2))]) }]), Chc([Choice { weight: None, value: Cat([Lit(Num(3))]) }])])]) }])
+Puts Chc(
+  Cat(
+    Tup(
+      Chc(
+        Cat(
+          Num(1)
+        )
+      )
+      Chc(
+        Cat(
+          Num(2)
+        )
+      )
+      Chc(
+        Cat(
+          Num(3)
+        )
+      )
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Range(Lit(Num(0)), Lit(Num(20)))]) }])
+Puts Chc(
+  Cat(
+    Range(
+      Num(0)
+      Num(20)
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Range(Var(SymbolU32 { value: 7 }), Var(SymbolU32 { value: 8 }))]) }])
+Puts Chc(
+  Cat(
+    Range(
+      Var(x)
+      Var(y)
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Ap(Var(SymbolU32 { value: 9 }), Var(SymbolU32 { value: 7 }))]) }])
+Puts Chc(
+  Cat(
+    Ap(
+      Var(f)
+      Var(x)
+    )
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Fun([Case { pat: Var(SymbolU32 { value: 7 }), expr: Chc([Choice { weight: None, value: Cat([Var(SymbolU32 { value: 7 })]) }]) }])]) }])
+Puts Chc(
+  Cat(
+    [???]
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Fun([Case { pat: Lit(Atom(SymbolU32 { value: 10 })), expr: Chc([Choice { weight: None, value: Cat([Lit(Str("yes"))]) }]) }, Case { pat: Lit(Atom(SymbolU32 { value: 11 })), expr: Chc([Choice { weight: None, value: Cat([Lit(Str("no"))]) }]) }])]) }])
+Puts Chc(
+  Cat(
+    [???]
+  )
+)
 

+ 5 - 1
tests/num_lit.parsed

@@ -1,2 +1,6 @@
-Chc([Choice { weight: None, value: Cat([Lit(Num(55))]) }])
+Puts Chc(
+  Cat(
+    Num(55)
+  )
+)
 

+ 30 - 5
tests/str_lit.parsed

@@ -1,10 +1,35 @@
-Chc([Choice { weight: None, value: Cat([Lit(Str("foo")), Lit(Str("foo"))]) }])
+Puts Chc(
+  Cat(
+    Str("foo")
+    Str("foo")
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Lit(Str("foo bar baz")), Lit(Str("foo bar baz"))]) }])
+Puts Chc(
+  Cat(
+    Str("foo bar baz")
+    Str("foo bar baz")
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Lit(Str("won't you")), Lit(Str("quoth the raven \"nevermore\""))]) }])
+Puts Chc(
+  Cat(
+    Str("won't you")
+    Str("quoth the raven \"nevermore\"")
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Lit(Str("this\nthat")), Lit(Str("one\ntwo"))]) }])
+Puts Chc(
+  Cat(
+    Str("this\nthat")
+    Str("one\ntwo")
+  )
+)
 
-Chc([Choice { weight: None, value: Cat([Lit(Str("\n\t\r")), Lit(Str("\n\t\r"))]) }])
+Puts Chc(
+  Cat(
+    Str("\n\t\r")
+    Str("\n\t\r")
+  )
+)