More rejiggering - tests still fail

This commit is contained in:
greg 2018-11-19 00:21:50 -08:00
parent 71dacc94d6
commit 0246e510ca

View File

@ -7,7 +7,29 @@ pub fn dispatch<V: ASTVisitor>(visitor: &mut V, ast: &AST) {
for statement in ast.0.iter() { for statement in ast.0.iter() {
match statement { match statement {
Statement::ExpressionStatement(e) => { Statement::ExpressionStatement(e) => {
match e { dispatch_expression(visitor, e);
visitor.expression(e);
},
Statement::Declaration(decl) => {
match decl {
Declaration::FuncSig(sig) => visitor.func_signature(sig),
Declaration::FuncDecl(sig, block) => visitor.func_declaration(sig, block),
Declaration::TypeDecl { name, body, mutable } => visitor.type_declaration(name, body, mutable),
Declaration::TypeAlias(alias, name) => visitor.type_alias(alias, name),
Declaration::Binding { name, constant, expr} => visitor.binding(name, constant, expr),
Declaration::Impl { type_name, interface_name, block } => visitor.impl_block(type_name, interface_name, block),
Declaration::Interface { name, signatures } => visitor.interface(name, signatures),
}
visitor.declaration(decl);
},
};
visitor.statement(statement);
}
visitor.ast(ast)
}
fn dispatch_expression<V: ASTVisitor>(visitor: &mut V, expression: &Expression) {
match expression {
Expression(expr, maybe_anno) => { Expression(expr, maybe_anno) => {
match expr { match expr {
ExpressionType::NatLiteral(n) => visitor.nat_literal(n), ExpressionType::NatLiteral(n) => visitor.nat_literal(n),
@ -31,26 +53,9 @@ pub fn dispatch<V: ASTVisitor>(visitor: &mut V, ast: &AST) {
visitor.expr_kind(expr); visitor.expr_kind(expr);
} }
} }
visitor.expression(e);
},
Statement::Declaration(decl) => {
match decl {
Declaration::FuncSig(sig) => visitor.func_signature(sig),
Declaration::FuncDecl(sig, block) => visitor.func_declaration(sig, block),
Declaration::TypeDecl { name, body, mutable } => visitor.type_declaration(name, body, mutable),
Declaration::TypeAlias(alias, name) => visitor.type_alias(alias, name),
Declaration::Binding { name, constant, expr} => visitor.binding(name, constant, expr),
Declaration::Impl { type_name, interface_name, block } => visitor.impl_block(type_name, interface_name, block),
Declaration::Interface { name, signatures } => visitor.interface(name, signatures),
}
visitor.declaration(decl);
},
};
visitor.statement(statement);
}
visitor.ast(ast)
} }
pub trait ASTVisitor { pub trait ASTVisitor {
fn ast(&mut self, _ast: &AST) { } fn ast(&mut self, _ast: &AST) { }
fn statement(&mut self, _stmt: &Statement) { } fn statement(&mut self, _stmt: &Statement) { }
@ -111,8 +116,12 @@ impl ASTVisitor for SchalaPrinter {
fn expression(&mut self, _: &Expression) { fn expression(&mut self, _: &Expression) {
self.s.push_str("some_expr"); self.s.push_str("some_expr");
} }
fn declaration(&mut self, _: &Declaration) {
self.s.push_str("some_decl"); fn binding(&mut self, name: &Rc<String>, constant: &bool, _expr: &Expression) {
self.s.push_str(&format!("let{} {} = {}",
if *constant { "" } else { " mut" },
name,
"some_expr"));
} }
} }
@ -136,7 +145,7 @@ mod visitor_tests {
let mut pp = SchalaPrinter::new(); let mut pp = SchalaPrinter::new();
dispatch(&mut pp, &ast); dispatch(&mut pp, &ast);
let result = pp.done(); let result = pp.done();
assert_eq!(result, r#"Pretty-printed AST assert_eq!(result, r#"Schala source code:
let a = 1 + 2 let a = 1 + 2
let b = 2 + 44 let b = 2 + 44
foo() foo()