Pretty printer sorta working

This commit is contained in:
greg 2018-11-17 22:34:42 -08:00
parent 1a934d7804
commit 4ded241c82

View File

@ -5,17 +5,17 @@ use ast::*;
pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T { pub fn dispatch<T, V: ASTVisitor<T>>(visitor: &mut V, ast: &AST) -> T {
visitor.ast(ast); visitor.ast(ast);
for statement in ast.0.iter() { for statement in ast.0.iter() {
visitor.statement(statement); let output = match statement {
match statement {
Statement::ExpressionStatement(expr) => { Statement::ExpressionStatement(expr) => {
let ref mut expression_visitor = visitor.expression(); let ref mut expression_visitor = visitor.expression();
ExpressionVisitor::dispatch(expression_visitor, expr); ExpressionVisitor::dispatch(expression_visitor, expr)
}, },
Statement::Declaration(decl) => { Statement::Declaration(decl) => {
let ref mut declaration_visitor = visitor.declaration(); let ref mut declaration_visitor = visitor.declaration();
DeclarationVisitor::dispatch(declaration_visitor, decl); DeclarationVisitor::dispatch(declaration_visitor, decl)
}, },
} };
visitor.statement(output);
} }
visitor.done() visitor.done()
} }
@ -25,7 +25,7 @@ pub trait ASTVisitor<T> {
type DV : DeclarationVisitor<T>; type DV : DeclarationVisitor<T>;
fn ast(&mut self, _ast: &AST) { } fn ast(&mut self, _ast: &AST) { }
fn statement(&mut self, _statement: &Statement) { } fn statement(&mut self, T) { }
fn expression(&mut self) -> Self::EV; fn expression(&mut self) -> Self::EV;
fn declaration(&mut self) -> Self::DV; fn declaration(&mut self) -> Self::DV;
fn done(&mut self) -> T; fn done(&mut self) -> T;
@ -48,9 +48,9 @@ pub trait DeclarationVisitor<T> {
FuncDecl(sig, block) => visitor.func_declaration(sig, block), FuncDecl(sig, block) => visitor.func_declaration(sig, block),
TypeDecl { name, body, mutable } => visitor.type_declaration(name, body, mutable), TypeDecl { name, body, mutable } => visitor.type_declaration(name, body, mutable),
TypeAlias(alias, name) => visitor.type_alias(alias, name), TypeAlias(alias, name) => visitor.type_alias(alias, name),
Binding { .. } => unimplemented!(), Binding { name, constant, expr} => visitor.binding(name, constant, expr),
Impl { .. } => unimplemented!(), Impl { type_name, interface_name, block } => visitor.impl_block(type_name, interface_name, block),
Interface { .. } => unimplemented!(), Interface { name, signatures } => visitor.interface(name, signatures),
}; };
visitor.done() visitor.done()
} }
@ -60,42 +60,12 @@ pub trait DeclarationVisitor<T> {
fn func_declaration(&mut self, _sig: &Signature, _block: &Vec<Statement>) { } fn func_declaration(&mut self, _sig: &Signature, _block: &Vec<Statement>) { }
fn type_declaration(&mut self, _name: &TypeSingletonName, _body: &TypeBody, _mutable: &bool) { } fn type_declaration(&mut self, _name: &TypeSingletonName, _body: &TypeBody, _mutable: &bool) { }
fn type_alias(&mut self, _alias: &Rc<String>, _name: &Rc<String>) { } fn type_alias(&mut self, _alias: &Rc<String>, _name: &Rc<String>) { }
fn binding(&mut self, _name: &Rc<String>, _constant: &bool, _expr: &Expression) { }
fn impl_block(&mut self, _type_name: &TypeIdentifier, _interface_name: &Option<InterfaceName>, _block: &Vec<Declaration>) { }
fn interface(&mut self, name: &Rc<String>, signatures: &Vec<Signature>) { }
fn done(&mut self) -> T; fn done(&mut self) -> T;
} }
/*----*/
struct NullVisitor { }
impl ASTVisitor<()> for NullVisitor {
type EV = NullVisitor;
type DV = NullVisitor;
fn done(&mut self) -> () {
()
}
fn expression(&mut self) -> Self::EV {
NullVisitor { }
}
fn declaration(&mut self) -> Self::DV {
NullVisitor { }
}
}
impl ExpressionVisitor<()> for NullVisitor {
fn done(&mut self) -> () {
()
}
}
impl DeclarationVisitor<()> for NullVisitor {
fn done(&mut self) -> () {
()
}
}
#[derive(Default, Clone)] #[derive(Default, Clone)]
struct SchalaPrinter { struct SchalaPrinter {
s: String s: String
@ -108,6 +78,11 @@ impl ASTVisitor<String> for SchalaPrinter {
self.s.push_str("Pretty-printed AST"); self.s.push_str("Pretty-printed AST");
} }
fn statement(&mut self, s: String) {
self.s.push_str("\n");
self.s.push_str(&s);
}
fn expression(&mut self) -> Self::EV { fn expression(&mut self) -> Self::EV {
SchalaPrinter::default() SchalaPrinter::default()
} }
@ -137,6 +112,7 @@ mod visitor_tests {
use ::tokenizing::{Token, tokenize}; use ::tokenizing::{Token, tokenize};
use ::parsing::ParseResult; use ::parsing::ParseResult;
use ::ast::AST; use ::ast::AST;
use super::*;
fn parse(input: &str) -> ParseResult<AST> { fn parse(input: &str) -> ParseResult<AST> {
let tokens = tokenize(input); let tokens = tokenize(input);
@ -146,6 +122,14 @@ mod visitor_tests {
#[test] #[test]
fn test() { fn test() {
let q = parse("foo"); let ast = parse("let a = 1 + 2; let b = 2 + 44;foo()").unwrap();
let mut pp = SchalaPrinter::default();
let result = dispatch(&mut pp, &ast);
assert_eq!(result, r#"Pretty-printed AST
let a = 1 + 2
let b = 2 + 44
foo()
"#);
} }
} }