Skip to content

Commit 605340b

Browse files
committed
Started implementing parsing for more C code constructs
1 parent d0a380f commit 605340b

File tree

3 files changed

+159
-20
lines changed

3 files changed

+159
-20
lines changed

Diff for: src/c/parser/mod.rs

+155-17
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub enum ParameterDeclarationCore {
112112

113113
#[derive(Clone, Debug)]
114114
pub struct ParameterDeclaration {
115-
pub attributes: Vec<()>,
115+
pub attributes: Vec<Attribute>,
116116
pub declaration_specifiers: DeclarationSpecifiers,
117117
pub core: ParameterDeclarationCore,
118118
pub source: Source,
@@ -176,7 +176,7 @@ pub struct FunctionQualifier {
176176

177177
#[derive(Clone, Debug)]
178178
pub struct Pointer {
179-
pub attributes: Vec<()>,
179+
pub attributes: Vec<Attribute>,
180180
pub type_qualifiers: Vec<TypeQualifier>,
181181
pub source: Source,
182182
}
@@ -291,7 +291,7 @@ impl TypeSpecifierQualifier {
291291

292292
#[derive(Clone, Debug)]
293293
pub struct SpecifierQualifierList {
294-
pub attributes: Vec<()>,
294+
pub attributes: Vec<Attribute>,
295295
pub type_specifier_qualifiers: Vec<TypeSpecifierQualifier>,
296296
}
297297

@@ -347,7 +347,7 @@ pub enum AlignmentSpecifierKind {
347347
#[derive(Clone, Debug)]
348348
pub struct DeclarationSpecifiers {
349349
pub specifiers: Vec<DeclarationSpecifier>,
350-
pub attributes: Vec<()>,
350+
pub attributes: Vec<Attribute>,
351351
}
352352

353353
impl From<&SpecifierQualifierList> for DeclarationSpecifiers {
@@ -379,7 +379,7 @@ pub struct StaticAssertDeclaration {
379379

380380
#[derive(Clone, Debug)]
381381
pub struct Member {
382-
pub attributes: Vec<()>,
382+
pub attributes: Vec<Attribute>,
383383
pub specifier_qualifiers: SpecifierQualifierList,
384384
pub member_declarators: Vec<MemberDeclarator>,
385385
}
@@ -396,16 +396,109 @@ pub enum ExternalDeclaration {
396396
FunctionDefinition(FunctionDefinition),
397397
}
398398

399+
#[derive(Clone, Debug, From)]
400+
pub enum BlockItem {
401+
ExternalDeclaration(ExternalDeclaration),
402+
UnlabeledStatement(UnlabeledStatement),
403+
Label(Label),
404+
}
405+
406+
#[derive(Clone, Debug, From)]
407+
pub enum UnlabeledStatement {
408+
ExprStatement(ExprStatement),
409+
PrimaryBlock(Vec<Attribute>, PrimaryBlock),
410+
JumpStatement(Vec<Attribute>, JumpStatement),
411+
}
412+
413+
#[derive(Clone, Debug)]
414+
pub enum ExprStatement {
415+
Empty,
416+
Normal(Vec<Attribute>, Expr),
417+
}
418+
419+
#[derive(Clone, Debug)]
420+
pub struct Attribute {
421+
pub kind: AttributeKind,
422+
pub clause: Vec<CToken>,
423+
}
424+
425+
#[derive(Clone, Debug)]
426+
pub enum AttributeKind {
427+
Standard(String),
428+
Namespaced(String, String),
429+
}
430+
431+
#[derive(Clone, Debug)]
432+
pub enum JumpStatement {
433+
Goto(String),
434+
Continue,
435+
Break,
436+
Return(Option<Expr>),
437+
}
438+
439+
#[derive(Clone, Debug)]
440+
pub enum PrimaryBlock {
441+
CompoundStatement(CompoundStatement),
442+
SelectionStatement(SelectionStatement),
443+
IterationStatement(IterationStatement),
444+
}
445+
446+
#[derive(Clone, Debug)]
447+
pub enum IterationStatement {
448+
While(Expr, SecondaryBlock),
449+
DoWhile(SecondaryBlock, Expr),
450+
For(Option<Expr>, Option<Expr>, Option<Expr>),
451+
}
452+
453+
#[derive(Clone, Debug)]
454+
pub struct CompoundStatement {
455+
pub statements: Vec<BlockItem>,
456+
}
457+
458+
type SecondaryBlock = Statement;
459+
460+
#[derive(Clone, Debug)]
461+
pub enum Statement {
462+
LabeledStatement(Box<LabeledStatement>),
463+
UnlabeledStatement(Box<UnlabeledStatement>),
464+
}
465+
466+
#[derive(Clone, Debug)]
467+
pub struct LabeledStatement {
468+
pub label: Label,
469+
pub statement: Statement,
470+
}
471+
472+
#[derive(Clone, Debug)]
473+
pub enum SelectionStatement {
474+
If(Expr, SecondaryBlock),
475+
IfElse(Expr, SecondaryBlock, SecondaryBlock),
476+
Switch(Expr, SecondaryBlock),
477+
}
478+
479+
#[derive(Clone, Debug)]
480+
pub enum LabelKind {
481+
UserDefined(String),
482+
Case(ConstExpr),
483+
Default,
484+
}
485+
486+
#[derive(Clone, Debug)]
487+
pub struct Label {
488+
pub attributes: Vec<Attribute>,
489+
pub kind: LabelKind,
490+
}
491+
399492
#[derive(Clone, Debug)]
400493
pub enum Declaration {
401494
Common(CommonDeclaration),
402495
StaticAssert(StaticAssertDeclaration),
403-
Attribute(Vec<()>),
496+
Attribute(Vec<Attribute>),
404497
}
405498

406499
#[derive(Clone, Debug)]
407500
pub struct CommonDeclaration {
408-
pub attribute_specifiers: Vec<()>,
501+
pub attribute_specifiers: Vec<Attribute>,
409502
pub declaration_specifiers: DeclarationSpecifiers,
410503
pub init_declarator_list: Vec<InitDeclarator>,
411504
}
@@ -429,7 +522,7 @@ pub struct Composite {
429522
pub kind: CompositeKind,
430523
pub source: Source,
431524
pub name: Option<String>,
432-
pub attributes: Vec<()>,
525+
pub attributes: Vec<Attribute>,
433526
pub members: Option<Vec<MemberDeclaration>>,
434527
}
435528

@@ -441,7 +534,7 @@ pub struct EnumTypeSpecifier {
441534
#[derive(Clone, Debug)]
442535
pub struct Enumerator {
443536
pub name: String,
444-
pub attributes: Vec<()>,
537+
pub attributes: Vec<Attribute>,
445538
pub value: Option<ConstExpr>,
446539
pub source: Source,
447540
}
@@ -464,7 +557,7 @@ impl Enumeration {
464557
#[derive(Clone, Debug)]
465558
pub struct EnumerationDefinition {
466559
pub name: Option<String>,
467-
pub attributes: Vec<()>,
560+
pub attributes: Vec<Attribute>,
468561
pub enum_type_specifier: Option<EnumTypeSpecifier>,
469562
pub body: Vec<Enumerator>,
470563
pub source: Source,
@@ -572,7 +665,7 @@ impl<'a> Parser<'a> {
572665
Ok(())
573666
}
574667

575-
fn parse_attribute_specifier_sequence(&mut self) -> Result<Vec<()>, ParseError> {
668+
fn parse_attribute_specifier_sequence(&mut self) -> Result<Vec<Attribute>, ParseError> {
576669
#[allow(clippy::never_loop)]
577670
while self.eat_sequence(&[
578671
CTokenKind::Punctuator(Punctuator::OpenBracket),
@@ -1541,27 +1634,72 @@ impl<'a> Parser<'a> {
15411634
}
15421635

15431636
fn parse_function_body(&mut self) -> Result<(), ParseError> {
1544-
self.parse_compound_statement()
1637+
let _statements = self.parse_compound_statement()?;
1638+
todo!("parse_function_body");
15451639
}
15461640

1547-
fn parse_compound_statement(&mut self) -> Result<(), ParseError> {
1641+
fn parse_compound_statement(&mut self) -> Result<CompoundStatement, ParseError> {
15481642
if !self.eat_punctuator(Punctuator::OpenCurly) {
15491643
return Err(ParseError::new(
15501644
ParseErrorKind::Misc("Expected '{' to begin compound statement"),
15511645
self.input.peek().source,
15521646
));
15531647
}
15541648

1555-
todo!("parse compound statement");
1649+
let mut statements = vec![];
1650+
1651+
loop {
1652+
if self.eat_punctuator(Punctuator::CloseCurly) {
1653+
break;
1654+
}
1655+
1656+
if let Ok(declaration) = self.parse_declaration() {
1657+
statements.push(declaration.into());
1658+
}
1659+
1660+
if let Ok(unlabeled_statement) = self.parse_unlabeled_statement() {
1661+
statements.push(unlabeled_statement.into());
1662+
}
1663+
1664+
if let Ok(label) = self.parse_label() {
1665+
statements.push(label.into());
1666+
}
15561667

1557-
if !self.eat_punctuator(Punctuator::CloseCurly) {
15581668
return Err(ParseError::new(
1559-
ParseErrorKind::Misc("Expected '}' to close compound statement"),
1669+
ParseErrorKind::Misc("Expected '}' to end compound statement"),
15601670
self.input.peek().source,
15611671
));
15621672
}
15631673

1564-
Ok(())
1674+
Ok(CompoundStatement { statements })
1675+
}
1676+
1677+
fn parse_unlabeled_statement(&mut self) -> Result<UnlabeledStatement, ParseError> {
1678+
if let Ok(expr_statement) = self.parse_expr_statement() {
1679+
return Ok(expr_statement.into());
1680+
}
1681+
1682+
todo!("parse_unlabeled_statement")
1683+
}
1684+
1685+
fn parse_expr_statement(&mut self) -> Result<ExprStatement, ParseError> {
1686+
if self
1687+
.input
1688+
.peek_is(CTokenKind::Punctuator(Punctuator::Semicolon))
1689+
{
1690+
return Ok(ExprStatement::Empty);
1691+
}
1692+
1693+
let attributes = self.parse_attribute_specifier_sequence()?;
1694+
1695+
return Ok(ExprStatement::Normal(
1696+
attributes,
1697+
self.parse_expr_multiple()?,
1698+
));
1699+
}
1700+
1701+
fn parse_label(&mut self) -> Result<Label, ParseError> {
1702+
todo!("parse_label")
15651703
}
15661704

15671705
fn eat(&mut self, expected: CTokenKind) -> bool {

Diff for: src/c/translation/function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
asg::TypeParams,
44
ast::{self, AstFile, Func, FuncHead, Param, Params},
55
c::parser::{
6-
error::ParseErrorKind, CTypedef, DeclarationSpecifiers, Declarator,
6+
error::ParseErrorKind, Attribute, CTypedef, DeclarationSpecifiers, Declarator,
77
ParameterDeclarationCore, ParameterTypeList, ParseError, StorageClassSpecifier,
88
},
99
diagnostics::Diagnostics,
@@ -14,7 +14,7 @@ use std::collections::HashMap;
1414
pub fn declare_function(
1515
typedefs: &mut HashMap<String, CTypedef>,
1616
ast_file: &mut AstFile,
17-
_attribute_specifiers: &[()],
17+
_attribute_specifiers: &[Attribute],
1818
declaration_specifiers: &DeclarationSpecifiers,
1919
declarator: &Declarator,
2020
parameter_type_list: &ParameterTypeList,

Diff for: src/c/translation/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod types;
66

77
use self::types::get_name_and_type;
88
pub use self::{expr::translate_expr, function::declare_function};
9+
use super::parser::Attribute;
910
use crate::{
1011
asg::TypeParams,
1112
ast::{self, AstFile},
@@ -18,7 +19,7 @@ use std::collections::HashMap;
1819
pub fn declare_named_declaration(
1920
ast_file: &mut AstFile,
2021
declarator: &Declarator,
21-
_attribute_specifiers: &[()],
22+
_attribute_specifiers: &[Attribute],
2223
declaration_specifiers: &DeclarationSpecifiers,
2324
typedefs: &mut HashMap<String, CTypedef>,
2425
diagnostics: &Diagnostics,

0 commit comments

Comments
 (0)