mirror of
https://github.com/Noratrieb/uwucc.git
synced 2026-01-14 16:45:07 +01:00
more stuff
This commit is contained in:
parent
77e18126e7
commit
c84fdfaf3a
12 changed files with 270 additions and 38 deletions
|
|
@ -484,6 +484,16 @@ where
|
|||
return self.if_statement();
|
||||
}
|
||||
|
||||
if let Some((_, span)) = eat!(self, Tok::Kw(Kw::Return)) {
|
||||
if let Some((_, semi_span)) = eat!(self, Tok::Punct(P::Semicolon)) {
|
||||
return Ok((Stmt::Return(None), span.extend(semi_span)));
|
||||
} else {
|
||||
let expr = self.expr()?;
|
||||
let semi_span = expect!(self, Tok::Punct(P::Semicolon));
|
||||
return Ok((Stmt::Return(Some(expr)), span.extend(semi_span)));
|
||||
}
|
||||
}
|
||||
|
||||
// it must be an expression stmt
|
||||
let (expr, span) = self.expr()?;
|
||||
expect!(self, Tok::Punct(P::Semicolon));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
---
|
||||
source: parser/src/parser/tests.rs
|
||||
expression: "(parsed_pretty, pretty_printed_source)"
|
||||
---
|
||||
(
|
||||
Ok([
|
||||
(
|
||||
FunctionDef(FunctionDef {
|
||||
decl: Normal(NormalDecl {
|
||||
decl_spec: DeclSpec {
|
||||
ty: Integer(IntTy { sign: Signed, kind: Int }),
|
||||
attrs: "(empty)",
|
||||
},
|
||||
init_declarators: [
|
||||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
},
|
||||
5..9,
|
||||
),
|
||||
],
|
||||
}),
|
||||
body: [(Return(None), 18..25)],
|
||||
}),
|
||||
1..27,
|
||||
),
|
||||
]),
|
||||
"int main() {\n return\n}\n",
|
||||
)
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
---
|
||||
source: parser/src/parser/tests.rs
|
||||
expression: "(parsed_pretty, pretty_printed_source)"
|
||||
---
|
||||
(
|
||||
Ok([
|
||||
(
|
||||
FunctionDef(FunctionDef {
|
||||
decl: Normal(NormalDecl {
|
||||
decl_spec: DeclSpec {
|
||||
ty: Integer(IntTy { sign: Signed, kind: Int }),
|
||||
attrs: "(empty)",
|
||||
},
|
||||
init_declarators: [
|
||||
(
|
||||
InitDecl {
|
||||
declarator: Declarator {
|
||||
decl: WithParams {
|
||||
ident: (main, 5..9),
|
||||
params: [],
|
||||
},
|
||||
pointer: false,
|
||||
},
|
||||
init: None,
|
||||
},
|
||||
5..9,
|
||||
),
|
||||
],
|
||||
}),
|
||||
body: [(Return(Some((Atom(Int(0)), 25..26))), 18..27)],
|
||||
}),
|
||||
1..29,
|
||||
),
|
||||
]),
|
||||
"int main() {\n return 0\n}\n",
|
||||
)
|
||||
|
|
@ -202,3 +202,25 @@ int main() {
|
|||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_empty() {
|
||||
parse_test!(
|
||||
r#"
|
||||
int main() {
|
||||
return;
|
||||
}
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_expr() {
|
||||
parse_test!(
|
||||
r#"
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -298,7 +298,7 @@ impl CLexExt for u8 {
|
|||
}
|
||||
|
||||
fn is_c_identifier_digit(&self) -> bool {
|
||||
matches!(self, b'0'..=b'9')
|
||||
self.is_ascii_digit()
|
||||
}
|
||||
|
||||
fn is_c_whitespace(&self) -> bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue