diff --git a/parser/src/ast.rs b/parser/src/ast.rs index a3d28ae..d889bae 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -113,7 +113,7 @@ pub enum Decl { #[derive(Debug, DebugPls)] pub struct InitDecl { pub declarator: Declarator, - pub init: Option, + pub init: Option>, } #[derive(Debug, DebugPls)] diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 89c3158..5e2b525 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -196,13 +196,13 @@ where first = false; let (declarator, span) = self.declarator()?; - if let Some((token, span)) = eat!(self, Tok::Punct(Punct::Eq)) { - return Err(ParserError::unsupported(span, &token)); - } - let init_decl = InitDecl { - declarator, - init: None, + let init = if eat!(self, Tok::Punct(Punct::Eq)).is_some() { + let expr = self.expr()?; + Some(expr) + } else { + None }; + let init_decl = InitDecl { declarator, init }; init_decls.push((init_decl, span)); } diff --git a/parser/src/parser/expr.rs b/parser/src/parser/expr.rs index 647e480..103b503 100644 --- a/parser/src/parser/expr.rs +++ b/parser/src/parser/expr.rs @@ -19,13 +19,13 @@ where } fn get_lhs(&mut self) -> Result> { - let (typ, span) = match self.peek_t()? { + let (typ, span) = match self.next_t()? { (Tok::Ident(ident), span) => (Atom::Ident(ident.to_string()), span), (Tok::StringLiteral(literal), span) => (Atom::String(literal.to_string()), span), - (Tok::Constant(Constant::Int(int)), span) => (Atom::Int(*int), span), - (Tok::Constant(Constant::Float(float)), span) => (Atom::Float(*float), span), - (Tok::Constant(Constant::Char(char)), span) => (Atom::Char(*char), span), - &(Tok::Punct(punct), span) => { + (Tok::Constant(Constant::Int(int)), span) => (Atom::Int(int), span), + (Tok::Constant(Constant::Float(float)), span) => (Atom::Float(float), span), + (Tok::Constant(Constant::Char(char)), span) => (Atom::Char(char), span), + (Tok::Punct(punct), span) => { let r_bp = prefix_binding_power(&Tok::Punct(punct)); let op = unary_op_from_token(&Tok::Punct(punct), span)?; let rhs = self.expr_bp(r_bp)?; @@ -40,13 +40,13 @@ where } (tok, span) => { return Err(ParserError::new( - *span, + span, format!("expected expression, found {tok}"), )); } }; - Ok((Expr::Atom(typ), *span)) + Ok((Expr::Atom(typ), span)) } fn expr_bp(&mut self, min_bp: u8) -> Result> { @@ -79,7 +79,7 @@ where ) } - todo!() + Ok(lhs) } } diff --git a/parser/src/parser/tests.rs b/parser/src/parser/tests.rs index 56ae862..fb6ec22 100644 --- a/parser/src/parser/tests.rs +++ b/parser/src/parser/tests.rs @@ -65,3 +65,12 @@ int function(); "# ); } + +#[test] +fn small_expression() { + parse_test!( + r#" +int x = 1 + 1; + "# + ); +} diff --git a/parser/src/pre/lexer.rs b/parser/src/pre/lexer.rs index f278295..e5772cd 100644 --- a/parser/src/pre/lexer.rs +++ b/parser/src/pre/lexer.rs @@ -385,6 +385,7 @@ where (b'.', _, _) => break (TokP(Punctuator::Dot), start_span), (b'&', _, _) => break (TokP(Punctuator::Ampersand), start_span), (b'*', _, _) => break (TokP(Punctuator::Asterisk), start_span), + (b'+', _, _) => break (TokP(Punctuator::Plus), start_span), (b'-', _, _) => break (TokP(Punctuator::Minus), start_span), (b'~', _, _) => break (TokP(Punctuator::Tilde), start_span), (b'!', _, _) => break (TokP(Punctuator::Bang), start_span), @@ -451,6 +452,8 @@ int main() { #[test] fn some_operators() { let src = r#" +int x = 1 + 1; + int hello(const char* uwu) <% uwu[5] <<= 23; *uwu * (p++); diff --git a/parser/src/pre/snapshots/parser__pre__lexer__tests__some_operators.snap b/parser/src/pre/snapshots/parser__pre__lexer__tests__some_operators.snap index 70218e3..c583845 100644 --- a/parser/src/pre/snapshots/parser__pre__lexer__tests__some_operators.snap +++ b/parser/src/pre/snapshots/parser__pre__lexer__tests__some_operators.snap @@ -9,166 +9,208 @@ expression: tokens ), 1..4, ), + ( + Identifier( + "x", + ), + 5..6, + ), + ( + Punctuator( + Eq, + ), + 7..8, + ), + ( + PpNumber( + "1", + ), + 9..10, + ), + ( + Punctuator( + Plus, + ), + 11..12, + ), + ( + PpNumber( + "1", + ), + 13..14, + ), + ( + Punctuator( + Semicolon, + ), + 14..15, + ), + ( + Identifier( + "int", + ), + 25..28, + ), ( Identifier( "hello", ), - 5..10, + 29..34, ), ( Punctuator( ParenOpen, ), - 10..11, + 34..35, ), ( Identifier( "const", ), - 11..16, + 35..40, ), ( Identifier( "char", ), - 17..21, + 41..45, ), ( Punctuator( Asterisk, ), - 21..22, + 45..46, ), ( Identifier( "uwu", ), - 23..26, + 47..50, ), ( Punctuator( ParenClose, ), - 26..27, + 50..51, ), ( Punctuator( BraceOpen, ), - 28..30, + 52..54, ), ( Identifier( "uwu", ), - 35..38, + 59..62, ), ( Punctuator( BracketOpen, ), - 38..39, + 62..63, ), ( PpNumber( "5", ), - 39..40, + 63..64, ), ( Punctuator( BracketClose, ), - 40..41, + 64..65, ), ( Punctuator( LeftLeftChevronEq, ), - 42..45, + 66..69, ), ( PpNumber( "23", ), - 46..48, + 70..72, ), ( Punctuator( Semicolon, ), - 48..49, + 72..73, ), ( Punctuator( Asterisk, ), - 54..55, + 78..79, ), ( Identifier( "uwu", ), - 55..58, + 79..82, ), ( Punctuator( Asterisk, ), - 59..60, + 83..84, ), ( Punctuator( ParenOpen, ), - 61..62, + 85..86, ), ( Identifier( "p", ), - 62..63, + 86..87, ), ( Punctuator( PlusPlus, ), - 63..65, + 87..89, ), ( Punctuator( ParenClose, ), - 65..66, + 89..90, ), ( Punctuator( Semicolon, ), - 66..67, + 90..91, ), ( Identifier( "return", ), - 72..78, + 96..102, ), ( Identifier( "p", ), - 79..80, + 103..104, ), ( Punctuator( Semicolon, ), - 80..81, + 104..105, ), ( Punctuator( BraceClose, ), - 82..84, + 106..108, ), ]