diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-11 19:08:25 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-11 19:08:25 -0500 |
| commit | 6bc1c9adfca016cdf2e1dd39425ca97a5ebd24b6 (patch) | |
| tree | 2d1897c85bc176b316db6d10aaa4a27835a7c586 | |
| parent | 02f9abad0419e84cf62497a31657a632c1ace5eb (diff) | |
| download | sloth-6bc1c9adfca016cdf2e1dd39425ca97a5ebd24b6.tar.gz | |
Tests for statements done, everything works great
| -rw-r--r-- | crates/sloth/src/parser/stmt.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/sloth/src/parser/stmt.rs b/crates/sloth/src/parser/stmt.rs index abb90fc..9049f6c 100644 --- a/crates/sloth/src/parser/stmt.rs +++ b/crates/sloth/src/parser/stmt.rs @@ -327,4 +327,36 @@ mod tests { assert_eq!(expected_ast, generated_ast); } + #[test] + fn basic_statement_e() { + let lexer = Lexer::new("if a+5 > 10 {\nprint(a);\n}"); + let tokens = lexer.collect_vec(); + println!("{tokens:?}"); + + let expected_ast = Stmt::If { + expr: (Expr::BinaryOp { + op: (BinaryOp::Gt), + lhs: (Box::new(Expr::BinaryOp { + op: (BinaryOp::Add), + lhs: (Box::new(Expr::Variable("a".to_string()))), + rhs: (Box::new(Expr::Literal(Literal::Integer(5)))), + })), + rhs: (Box::new(Expr::Literal(Literal::Integer(10)))), + }), + body: (vec![Stmt::ExprStmt(Expr::Call { + ident: (Box::new(Expr::Literal(Literal::String("print".to_string())))), + args: (vec![Expr::Variable("a".to_string())]), + })]), + else_if: (Vec::new()), + els: (None), + }; + + let mut parser = AstParser::new(tokens); + let generated_ast = parser.statement(); + + println!("Expected AST:\n{expected_ast:#?}\n\n"); + println!("Generated AST:\n{generated_ast:#?}\n\n"); + + assert_eq!(expected_ast, generated_ast); + } } |
