Parses a Yul statement. This function matches a grammar rule and return an Expr struct which is later added into the Abstract Syntax Tree
(expression: Pair<Rule>)
| 52 | //Parses a Yul statement. This function matches a grammar rule and return an Expr struct |
| 53 | //which is later added into the Abstract Syntax Tree |
| 54 | fn parse_statement(expression: Pair<Rule>) -> Expr { |
| 55 | let inner = expression.into_inner().next().unwrap(); |
| 56 | match inner.as_rule() { |
| 57 | //Rule is expr |
| 58 | Rule::expr => parse_expression(inner), |
| 59 | |
| 60 | //Rule is block |
| 61 | Rule::block => Expr::Block(parse_block(inner)), |
| 62 | |
| 63 | // Rule is code |
| 64 | Rule::code => Expr::Block(parse_block(inner.into_inner().next().unwrap())), |
| 65 | |
| 66 | //If the rule is a function definition, parse the function name, parameters, returns and then return an Expr |
| 67 | Rule::function_definition => { |
| 68 | let mut parts = inner.into_inner(); |
| 69 | let function_name = parts.next().unwrap().as_str(); |
| 70 | |
| 71 | //get the typed identifiers from the function and parse each expression |
| 72 | let params: Vec<TypedIdentifier> = parse_typed_identifier_list(parts.next().unwrap()); |
| 73 | let returns_rule = parts.next().unwrap(); |
| 74 | let mut returns = vec![]; |
| 75 | if let Some(inner) = returns_rule.into_inner().next() { |
| 76 | returns = parse_typed_identifier_list(inner); |
| 77 | } |
| 78 | |
| 79 | let block = parts.next().unwrap(); |
| 80 | |
| 81 | Expr::FunctionDefinition(ExprFunctionDefinition { |
| 82 | function_name: function_name.to_string(), |
| 83 | params, |
| 84 | returns, |
| 85 | block: parse_block(block), |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | //Rule is assignment |
| 90 | Rule::assignment => { |
| 91 | let mut parts = inner.into_inner(); |
| 92 | let identifiers = parse_identifier_list(parts.next().unwrap()); |
| 93 | let rhs = parts.next().unwrap(); |
| 94 | let rhs_expr = parse_expression(rhs); |
| 95 | Expr::Assignment(ExprAssignment { |
| 96 | identifiers, |
| 97 | inferred_types: vec![], |
| 98 | rhs: Box::new(rhs_expr), |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | //Rule is if statement |
| 103 | Rule::if_statement => { |
| 104 | let mut inners = inner.into_inner(); |
| 105 | let first_arg = inners.next().unwrap(); |
| 106 | let second_arg = inners.next().unwrap(); |
| 107 | Expr::IfStatement(ExprIfStatement { |
| 108 | first_expr: Box::new(parse_expression(first_arg)), |
| 109 | second_expr: Box::new(parse_block(second_arg)), |
| 110 | }) |
| 111 | } |
no test coverage detected