More rejiggering - tests still fail
This commit is contained in:
parent
71dacc94d6
commit
0246e510ca
@ -7,30 +7,7 @@ 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);
|
||||||
Expression(expr, maybe_anno) => {
|
|
||||||
match expr {
|
|
||||||
ExpressionType::NatLiteral(n) => visitor.nat_literal(n),
|
|
||||||
ExpressionType::FloatLiteral(f) => visitor.float_literal(f),
|
|
||||||
ExpressionType::StringLiteral(s) => visitor.string_literal(s),
|
|
||||||
ExpressionType::BoolLiteral(b) => visitor.bool_literal(b),
|
|
||||||
ExpressionType::BinExp(binop, lhs, rhs) => visitor.binop(binop, lhs, rhs),
|
|
||||||
ExpressionType::PrefixExp(prefix, expr) => visitor.prefixop(prefix, expr),
|
|
||||||
ExpressionType::TupleLiteral(v) => visitor.tuple_literal(v),
|
|
||||||
ExpressionType::Value(v) => visitor.value(v),
|
|
||||||
ExpressionType::NamedStruct { name, fields } => visitor.named_struct(name, fields),
|
|
||||||
ExpressionType::Call { f, arguments } => visitor.call(f, arguments),
|
|
||||||
ExpressionType::Index { indexee, indexers } => visitor.index(indexee, indexers),
|
|
||||||
ExpressionType::IfExpression { discriminator, body } => visitor.if_expression(discriminator, body),
|
|
||||||
ExpressionType::WhileExpression { condition, body } => visitor.while_expresssion(condition, body),
|
|
||||||
ExpressionType::ForExpression { enumerators, body } => visitor.for_expression(enumerators, body),
|
|
||||||
ExpressionType::Lambda { params, type_anno, body } => visitor.lambda_expression(params, type_anno, body),
|
|
||||||
ExpressionType::ListLiteral(items) => visitor.list_literal(items),
|
|
||||||
}
|
|
||||||
visitor.anno_expr(maybe_anno);
|
|
||||||
visitor.expr_kind(expr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
visitor.expression(e);
|
visitor.expression(e);
|
||||||
},
|
},
|
||||||
Statement::Declaration(decl) => {
|
Statement::Declaration(decl) => {
|
||||||
@ -51,6 +28,34 @@ pub fn dispatch<V: ASTVisitor>(visitor: &mut V, ast: &AST) {
|
|||||||
visitor.ast(ast)
|
visitor.ast(ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dispatch_expression<V: ASTVisitor>(visitor: &mut V, expression: &Expression) {
|
||||||
|
match expression {
|
||||||
|
Expression(expr, maybe_anno) => {
|
||||||
|
match expr {
|
||||||
|
ExpressionType::NatLiteral(n) => visitor.nat_literal(n),
|
||||||
|
ExpressionType::FloatLiteral(f) => visitor.float_literal(f),
|
||||||
|
ExpressionType::StringLiteral(s) => visitor.string_literal(s),
|
||||||
|
ExpressionType::BoolLiteral(b) => visitor.bool_literal(b),
|
||||||
|
ExpressionType::BinExp(binop, lhs, rhs) => visitor.binop(binop, lhs, rhs),
|
||||||
|
ExpressionType::PrefixExp(prefix, expr) => visitor.prefixop(prefix, expr),
|
||||||
|
ExpressionType::TupleLiteral(v) => visitor.tuple_literal(v),
|
||||||
|
ExpressionType::Value(v) => visitor.value(v),
|
||||||
|
ExpressionType::NamedStruct { name, fields } => visitor.named_struct(name, fields),
|
||||||
|
ExpressionType::Call { f, arguments } => visitor.call(f, arguments),
|
||||||
|
ExpressionType::Index { indexee, indexers } => visitor.index(indexee, indexers),
|
||||||
|
ExpressionType::IfExpression { discriminator, body } => visitor.if_expression(discriminator, body),
|
||||||
|
ExpressionType::WhileExpression { condition, body } => visitor.while_expresssion(condition, body),
|
||||||
|
ExpressionType::ForExpression { enumerators, body } => visitor.for_expression(enumerators, body),
|
||||||
|
ExpressionType::Lambda { params, type_anno, body } => visitor.lambda_expression(params, type_anno, body),
|
||||||
|
ExpressionType::ListLiteral(items) => visitor.list_literal(items),
|
||||||
|
}
|
||||||
|
visitor.anno_expr(maybe_anno);
|
||||||
|
visitor.expr_kind(expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user