valhallac

Compiler for set-theoretic programming language.
git clone git://git.knutsen.co/valhallac
Log | Files | Refs | README | LICENSE

commit 28cc46cf32cf377f3a24374404e0f17e289171ba
parent ec6e098d84419337de4614f0fb6de1d4942924c3
Author: Demonstrandum <moi@knutsen.co>
Date:   Thu, 18 Jul 2019 15:39:26 +0100

Added pretty printing.

Diffstat:
Msrc/syntax/ast.rs | 30++++++++++++++++++++++++++----
Msrc/syntax/mod.rs | 2+-
Mtest.vh | 4++--
3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs @@ -159,8 +159,8 @@ impl fmt::Display for Nodes { Nodes::Str(node) => format!("%str{{ :value \"{}\" }}", node.value), Nodes::Sym(node) => format!("%sym{{ :value \"{}\" }}", node.value), Nodes::Call(node) => format!( - "%call{{ :callee ({}) :operands [| {} |] }}", node.callee, - node.operands.iter().map(Nodes::to_string).collect::<Vec<String>>().join(" ")), + "%call{{\n :callee ({})\n :operands [|\n {}\n |]\n}}", node.callee, + node.operands.iter().map(Nodes::to_string).collect::<Vec<String>>().join("\n ")), Nodes::Block(node) => format!("%block{{ ... }}"), }; write!(f, "{}", printable) @@ -235,9 +235,31 @@ impl Root { Root { branches: Vec::new() } } } + +const TAB : &str = " "; + +pub fn pretty_print(node : &Nodes, depth : usize) -> String { + let tab = TAB.repeat(depth); + let printable = match node { + Nodes::Ident(_) => format!("{}{}", tab, node), + Nodes::Num(_) => format!("{}{}", tab, node), + Nodes::Str(_) => format!("{}{}", tab, node), + Nodes::Sym(_) => format!("{}{}", tab, node), + Nodes::Call(n) => format!( + "{tab}%call{{\n{tab}{T}:callee (\n{calling}\n{tab}{T})\n{tab}{T}:operands [|\n{ops}\n{tab}{T}|]\n{tab}}}", + tab=tab, T=TAB, + calling=pretty_print(&*n.callee, depth + 2), + ops=n.operands.iter().map(|e| pretty_print(e, depth + 2)).collect::<Vec<String>>().join("\n") + ), + Nodes::Block(n) => format!("%block{{ ... }}"), + }; + printable +} + + impl fmt::Display for Root { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let str_mapped : Vec<String> = self.branches.iter().map(Nodes::to_string).collect(); - write!(f, "[|\n {}\n|]", str_mapped.join(",\n ")) + let str_mapped : Vec<String> = self.branches.iter().map(|n| pretty_print(n, 0)).collect(); + write!(f, "[|\n {}\n|]", str_mapped.join("\n").split("\n").collect::<Vec<&str>>().join("\n ")) } } \ No newline at end of file diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs @@ -33,5 +33,5 @@ pub fn parse_file(filename : &str) { println!("Stream:\n{}\n", stream.to_string()); let tree = parser::parse(stream); - println!("AST:\n{}\n", tree) + println!("AST:\n{}\n", tree); } \ No newline at end of file diff --git a/test.vh b/test.vh @@ -1 +1 @@ -- 8- \ No newline at end of file +a - b - c - d+ \ No newline at end of file