This commit is contained in:
nora 2022-06-26 13:06:30 +02:00
parent dc200d2046
commit 37772658bd
6 changed files with 96 additions and 42 deletions

View file

@ -113,7 +113,7 @@ pub enum Decl {
#[derive(Debug, DebugPls)] #[derive(Debug, DebugPls)]
pub struct InitDecl { pub struct InitDecl {
pub declarator: Declarator, pub declarator: Declarator,
pub init: Option<Expr>, pub init: Option<Spanned<Expr>>,
} }
#[derive(Debug, DebugPls)] #[derive(Debug, DebugPls)]

View file

@ -196,13 +196,13 @@ where
first = false; first = false;
let (declarator, span) = self.declarator()?; let (declarator, span) = self.declarator()?;
if let Some((token, span)) = eat!(self, Tok::Punct(Punct::Eq)) { let init = if eat!(self, Tok::Punct(Punct::Eq)).is_some() {
return Err(ParserError::unsupported(span, &token)); let expr = self.expr()?;
} Some(expr)
let init_decl = InitDecl { } else {
declarator, None
init: None,
}; };
let init_decl = InitDecl { declarator, init };
init_decls.push((init_decl, span)); init_decls.push((init_decl, span));
} }

View file

@ -19,13 +19,13 @@ where
} }
fn get_lhs(&mut self) -> Result<Spanned<Expr>> { fn get_lhs(&mut self) -> Result<Spanned<Expr>> {
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::Ident(ident), span) => (Atom::Ident(ident.to_string()), span),
(Tok::StringLiteral(literal), span) => (Atom::String(literal.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::Int(int)), span) => (Atom::Int(int), span),
(Tok::Constant(Constant::Float(float)), span) => (Atom::Float(*float), span), (Tok::Constant(Constant::Float(float)), span) => (Atom::Float(float), span),
(Tok::Constant(Constant::Char(char)), span) => (Atom::Char(*char), span), (Tok::Constant(Constant::Char(char)), span) => (Atom::Char(char), span),
&(Tok::Punct(punct), span) => { (Tok::Punct(punct), span) => {
let r_bp = prefix_binding_power(&Tok::Punct(punct)); let r_bp = prefix_binding_power(&Tok::Punct(punct));
let op = unary_op_from_token(&Tok::Punct(punct), span)?; let op = unary_op_from_token(&Tok::Punct(punct), span)?;
let rhs = self.expr_bp(r_bp)?; let rhs = self.expr_bp(r_bp)?;
@ -40,13 +40,13 @@ where
} }
(tok, span) => { (tok, span) => {
return Err(ParserError::new( return Err(ParserError::new(
*span, span,
format!("expected expression, found {tok}"), format!("expected expression, found {tok}"),
)); ));
} }
}; };
Ok((Expr::Atom(typ), *span)) Ok((Expr::Atom(typ), span))
} }
fn expr_bp(&mut self, min_bp: u8) -> Result<Spanned<Expr>> { fn expr_bp(&mut self, min_bp: u8) -> Result<Spanned<Expr>> {
@ -79,7 +79,7 @@ where
) )
} }
todo!() Ok(lhs)
} }
} }

View file

@ -65,3 +65,12 @@ int function();
"# "#
); );
} }
#[test]
fn small_expression() {
parse_test!(
r#"
int x = 1 + 1;
"#
);
}

View file

@ -385,6 +385,7 @@ where
(b'.', _, _) => break (TokP(Punctuator::Dot), start_span), (b'.', _, _) => break (TokP(Punctuator::Dot), start_span),
(b'&', _, _) => break (TokP(Punctuator::Ampersand), start_span), (b'&', _, _) => break (TokP(Punctuator::Ampersand), start_span),
(b'*', _, _) => break (TokP(Punctuator::Asterisk), 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::Minus), start_span),
(b'~', _, _) => break (TokP(Punctuator::Tilde), start_span), (b'~', _, _) => break (TokP(Punctuator::Tilde), start_span),
(b'!', _, _) => break (TokP(Punctuator::Bang), start_span), (b'!', _, _) => break (TokP(Punctuator::Bang), start_span),
@ -451,6 +452,8 @@ int main() {
#[test] #[test]
fn some_operators() { fn some_operators() {
let src = r#" let src = r#"
int x = 1 + 1;
int hello(const char* uwu) <% int hello(const char* uwu) <%
uwu[5] <<= 23; uwu[5] <<= 23;
*uwu * (p++); *uwu * (p++);

View file

@ -9,166 +9,208 @@ expression: tokens
), ),
1..4, 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( Identifier(
"hello", "hello",
), ),
5..10, 29..34,
), ),
( (
Punctuator( Punctuator(
ParenOpen, ParenOpen,
), ),
10..11, 34..35,
), ),
( (
Identifier( Identifier(
"const", "const",
), ),
11..16, 35..40,
), ),
( (
Identifier( Identifier(
"char", "char",
), ),
17..21, 41..45,
), ),
( (
Punctuator( Punctuator(
Asterisk, Asterisk,
), ),
21..22, 45..46,
), ),
( (
Identifier( Identifier(
"uwu", "uwu",
), ),
23..26, 47..50,
), ),
( (
Punctuator( Punctuator(
ParenClose, ParenClose,
), ),
26..27, 50..51,
), ),
( (
Punctuator( Punctuator(
BraceOpen, BraceOpen,
), ),
28..30, 52..54,
), ),
( (
Identifier( Identifier(
"uwu", "uwu",
), ),
35..38, 59..62,
), ),
( (
Punctuator( Punctuator(
BracketOpen, BracketOpen,
), ),
38..39, 62..63,
), ),
( (
PpNumber( PpNumber(
"5", "5",
), ),
39..40, 63..64,
), ),
( (
Punctuator( Punctuator(
BracketClose, BracketClose,
), ),
40..41, 64..65,
), ),
( (
Punctuator( Punctuator(
LeftLeftChevronEq, LeftLeftChevronEq,
), ),
42..45, 66..69,
), ),
( (
PpNumber( PpNumber(
"23", "23",
), ),
46..48, 70..72,
), ),
( (
Punctuator( Punctuator(
Semicolon, Semicolon,
), ),
48..49, 72..73,
), ),
( (
Punctuator( Punctuator(
Asterisk, Asterisk,
), ),
54..55, 78..79,
), ),
( (
Identifier( Identifier(
"uwu", "uwu",
), ),
55..58, 79..82,
), ),
( (
Punctuator( Punctuator(
Asterisk, Asterisk,
), ),
59..60, 83..84,
), ),
( (
Punctuator( Punctuator(
ParenOpen, ParenOpen,
), ),
61..62, 85..86,
), ),
( (
Identifier( Identifier(
"p", "p",
), ),
62..63, 86..87,
), ),
( (
Punctuator( Punctuator(
PlusPlus, PlusPlus,
), ),
63..65, 87..89,
), ),
( (
Punctuator( Punctuator(
ParenClose, ParenClose,
), ),
65..66, 89..90,
), ),
( (
Punctuator( Punctuator(
Semicolon, Semicolon,
), ),
66..67, 90..91,
), ),
( (
Identifier( Identifier(
"return", "return",
), ),
72..78, 96..102,
), ),
( (
Identifier( Identifier(
"p", "p",
), ),
79..80, 103..104,
), ),
( (
Punctuator( Punctuator(
Semicolon, Semicolon,
), ),
80..81, 104..105,
), ),
( (
Punctuator( Punctuator(
BraceClose, BraceClose,
), ),
82..84, 106..108,
), ),
] ]