diff --git a/schala-lang/language/src/ast/visitor.rs b/schala-lang/language/src/ast/visitor.rs index 05fc157..7291313 100644 --- a/schala-lang/language/src/ast/visitor.rs +++ b/schala-lang/language/src/ast/visitor.rs @@ -39,3 +39,33 @@ pub trait ASTVisitor: Sized { fn prefix_exp(&mut self, _op: &PrefixOp, _arg: &Expression) {} fn pattern(&mut self, _pat: &Pattern) {} } + +pub trait ExpressionVisitor { + fn type_anno(&mut self, _anno: &TypeIdentifier) -> T; + fn nat_literal(&mut self, _value: &u64) -> T; + fn string_literal(&mut self, _value: &Rc) -> T; + fn binexp(&mut self, _op: &BinOp, _lhs: &Expression, _rhs: &Expression) -> T; + fn done(&mut self, kind: T, anno: Option) -> T; +} + +pub fn dispatch_expression_kind_visitor(input: &AST, visitor: &mut dyn ExpressionVisitor) -> Result { + if input.statements.len() != 1 { + return Err(format!("One statement only!")); + } + let expr = match input.statements[0].kind { + StatementKind::Expression(ref expr) => expr, + _ => return Err(format!("Single statement needs to be an expr!")), + }; + + let output = match expr.kind { + ExpressionKind::NatLiteral(ref n) => visitor.nat_literal(n), + ExpressionKind::StringLiteral(ref s) => visitor.string_literal(s), + ExpressionKind::BinExp(ref op, ref lhs, ref rhs) => { + visitor.binexp(op, lhs, rhs) + }, + _ => return Err(format!("Lol not done yet!")), + }; + + let type_output = expr.type_anno.as_ref().map(|anno| visitor.type_anno(anno)); + Ok(visitor.done(output, type_output)) +} diff --git a/schala-lang/language/src/ast/visitor_test.rs b/schala-lang/language/src/ast/visitor_test.rs index 7c9246c..feaa2ef 100644 --- a/schala-lang/language/src/ast/visitor_test.rs +++ b/schala-lang/language/src/ast/visitor_test.rs @@ -39,3 +39,15 @@ fn heh() { assert_eq!(tester.count, 6); assert_eq!(tester.float_count, 1); } + + +struct NewTester { +} + + +#[test] +fn new_visitor() { + + + +}