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)]
pub struct InitDecl {
pub declarator: Declarator,
pub init: Option<Expr>,
pub init: Option<Spanned<Expr>>,
}
#[derive(Debug, DebugPls)]

View file

@ -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));
}

View file

@ -19,13 +19,13 @@ where
}
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::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<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::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++);

View file

@ -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,
),
]