Skip to content

Commit 378fe1b

Browse files
committed
Continued working on C parsing for function bodies
1 parent 605340b commit 378fe1b

File tree

5 files changed

+82
-37
lines changed

5 files changed

+82
-37
lines changed

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

+36-8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub enum ExprKind {
4646
Negate(Box<Expr>),
4747
BitComplement(Box<Expr>),
4848
Not(Box<Expr>),
49+
Call(Box<Expr>, Vec<Expr>),
4950
}
5051

5152
impl ExprKind {
@@ -274,7 +275,7 @@ impl<'a> Parser<'a> {
274275
todo!()
275276
}
276277

277-
Err(ParseErrorKind::Misc("Expected expression atom").at(source))
278+
Err(ParseErrorKind::Misc("Expected expression").at(source))
278279
}
279280

280281
pub fn parse_expr_primary(&mut self) -> Result<Expr, ParseError> {
@@ -284,9 +285,10 @@ impl<'a> Parser<'a> {
284285

285286
fn parse_expr_post(&mut self, base: Expr) -> Result<Expr, ParseError> {
286287
let mut base = base;
288+
let source = self.input.peek().source;
287289

288290
loop {
289-
if let Some(source) = self.eat_punctuator_source(Punctuator::OpenBracket) {
291+
if self.eat_punctuator(Punctuator::OpenBracket) {
290292
let subscript = self.parse_expr_multiple()?;
291293

292294
if !self.eat_punctuator(Punctuator::CloseBracket) {
@@ -303,7 +305,7 @@ impl<'a> Parser<'a> {
303305
continue;
304306
}
305307

306-
if let Some(source) = self.eat_punctuator_source(Punctuator::Dot) {
308+
if self.eat_punctuator(Punctuator::Dot) {
307309
let field = self.eat_identifier().ok_or_else(|| {
308310
ParseErrorKind::Misc("Expected field name after '.'").at(source)
309311
})?;
@@ -318,7 +320,7 @@ impl<'a> Parser<'a> {
318320
continue;
319321
}
320322

321-
if let Some(source) = self.eat_punctuator_source(Punctuator::Arrow) {
323+
if self.eat_punctuator(Punctuator::Arrow) {
322324
let field = self.eat_identifier().ok_or_else(|| {
323325
ParseErrorKind::Misc("Expected field name after '.'").at(source)
324326
})?;
@@ -333,19 +335,19 @@ impl<'a> Parser<'a> {
333335
continue;
334336
}
335337

336-
if let Some(source) = self.eat_punctuator_source(Punctuator::Increment) {
338+
if self.eat_punctuator(Punctuator::Increment) {
337339
base = ExprKind::PostIncrement(Box::new(base)).at(source);
338340
continue;
339341
}
340342

341-
if let Some(source) = self.eat_punctuator_source(Punctuator::Decrement) {
343+
if self.eat_punctuator(Punctuator::Decrement) {
342344
base = ExprKind::PostDecrement(Box::new(base)).at(source);
343345
continue;
344346
}
345347

346348
if self.eat_open_paren() {
347-
// Call
348-
base = todo!();
349+
let args = self.parse_argument_expr_list_rest()?;
350+
base = ExprKind::Call(Box::new(base), args).at(source);
349351
continue;
350352
}
351353

@@ -355,6 +357,32 @@ impl<'a> Parser<'a> {
355357
Ok(base)
356358
}
357359

360+
pub fn parse_argument_expr_list_rest(&mut self) -> Result<Vec<Expr>, ParseError> {
361+
// function_name_or_value(arg1, arg2, arg3, arg4, argN)
362+
// ^
363+
364+
let mut args = vec![];
365+
366+
if self.eat_punctuator(Punctuator::CloseParen) {
367+
return Ok(args);
368+
}
369+
370+
loop {
371+
args.push(self.parse_expr_singular()?);
372+
373+
if self.eat_punctuator(Punctuator::Comma) {
374+
continue;
375+
}
376+
377+
if self.eat_punctuator(Punctuator::CloseParen) {
378+
return Ok(args);
379+
}
380+
381+
return Err(ParseErrorKind::Misc("Expected ')' to close argument list")
382+
.at(self.input.peek().source));
383+
}
384+
}
385+
358386
pub fn parse_expr_primary_base(&mut self) -> Result<Expr, ParseError> {
359387
// Parse sequence of unary operators and casts
360388

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

+42-20
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,15 @@ pub enum MemberDeclarator {
390390
BitField(Option<Declarator>, ConstExpr),
391391
}
392392

393-
#[derive(Clone, Debug)]
393+
#[derive(Clone, Debug, From)]
394394
pub enum ExternalDeclaration {
395395
Declaration(Declaration),
396396
FunctionDefinition(FunctionDefinition),
397397
}
398398

399399
#[derive(Clone, Debug, From)]
400400
pub enum BlockItem {
401-
ExternalDeclaration(ExternalDeclaration),
401+
Declaration(Declaration),
402402
UnlabeledStatement(UnlabeledStatement),
403403
Label(Label),
404404
}
@@ -650,19 +650,19 @@ impl<'a> Parser<'a> {
650650
}
651651

652652
fn parse_external_declaration(&mut self) -> Result<ExternalDeclaration, ParseError> {
653-
if let Ok(_func_definition) = speculate!(self.input, self.parse_function_definition()) {
654-
return Ok(todo!());
653+
if let Ok(declaration) = speculate!(self.input, self.parse_declaration()) {
654+
return Ok(declaration.into());
655655
}
656656

657-
speculate!(self.input, self.parse_declaration())
657+
Ok(speculate!(self.input, self.parse_function_definition())?.into())
658658
}
659659

660-
fn parse_function_definition(&mut self) -> Result<(), ParseError> {
660+
fn parse_function_definition(&mut self) -> Result<FunctionDefinition, ParseError> {
661661
self.parse_attribute_specifier_sequence()?;
662662
self.parse_declaration_specifiers()?;
663663
self.parse_declarator()?;
664664
self.parse_function_body()?;
665-
Ok(())
665+
Ok(todo!("parse_function_definition"))
666666
}
667667

668668
fn parse_attribute_specifier_sequence(&mut self) -> Result<Vec<Attribute>, ParseError> {
@@ -1462,18 +1462,16 @@ impl<'a> Parser<'a> {
14621462
todo!()
14631463
}
14641464

1465-
fn parse_declaration(&mut self) -> Result<ExternalDeclaration, ParseError> {
1465+
fn parse_declaration(&mut self) -> Result<Declaration, ParseError> {
14661466
if self.input.peek_is(CTokenKind::StaticAssertKeyword) {
1467-
return Ok(ExternalDeclaration::Declaration(Declaration::StaticAssert(
1468-
self.parse_static_assert()?,
1469-
)));
1467+
return Ok(Declaration::StaticAssert(self.parse_static_assert()?));
14701468
}
14711469

14721470
let attribute_specifiers = self.parse_attribute_specifier_sequence()?;
14731471

14741472
if self.eat_punctuator(Punctuator::Semicolon) {
14751473
// attribute-declaration
1476-
todo!();
1474+
todo!("parse attribute declaration");
14771475
return Ok(todo!());
14781476
}
14791477

@@ -1496,13 +1494,11 @@ impl<'a> Parser<'a> {
14961494
);
14971495
}
14981496

1499-
Ok(ExternalDeclaration::Declaration(Declaration::Common(
1500-
CommonDeclaration {
1501-
attribute_specifiers,
1502-
declaration_specifiers,
1503-
init_declarator_list,
1504-
},
1505-
)))
1497+
Ok(Declaration::Common(CommonDeclaration {
1498+
attribute_specifiers,
1499+
declaration_specifiers,
1500+
init_declarator_list,
1501+
}))
15061502
}
15071503

15081504
fn parse_init_declarator_list(&mut self) -> Result<Vec<InitDeclarator>, ParseError> {
@@ -1655,14 +1651,17 @@ impl<'a> Parser<'a> {
16551651

16561652
if let Ok(declaration) = self.parse_declaration() {
16571653
statements.push(declaration.into());
1654+
continue;
16581655
}
16591656

16601657
if let Ok(unlabeled_statement) = self.parse_unlabeled_statement() {
16611658
statements.push(unlabeled_statement.into());
1659+
continue;
16621660
}
16631661

16641662
if let Ok(label) = self.parse_label() {
16651663
statements.push(label.into());
1664+
continue;
16661665
}
16671666

16681667
return Err(ParseError::new(
@@ -1699,7 +1698,30 @@ impl<'a> Parser<'a> {
16991698
}
17001699

17011700
fn parse_label(&mut self) -> Result<Label, ParseError> {
1702-
todo!("parse_label")
1701+
let attributes = self.parse_attribute_specifier_sequence()?;
1702+
1703+
if self.eat(CTokenKind::CaseKeyword) {
1704+
return Ok(Label {
1705+
attributes,
1706+
kind: LabelKind::Case(self.parse_constant_expression()?),
1707+
});
1708+
}
1709+
1710+
if self.eat(CTokenKind::DefaultKeyword) {
1711+
return Ok(Label {
1712+
attributes,
1713+
kind: LabelKind::Default,
1714+
});
1715+
}
1716+
1717+
if let Some(label) = self.eat_identifier() {
1718+
return Ok(Label {
1719+
attributes,
1720+
kind: LabelKind::UserDefined(label),
1721+
});
1722+
}
1723+
1724+
return Err(ParseErrorKind::Misc("Expected label").at(self.input.peek().source));
17031725
}
17041726

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub fn evaluate_to_const_integer(expr: &Expr) -> Result<BigInt, ParseError> {
5050
| ExprKind::Dereference(_)
5151
| ExprKind::Negate(_)
5252
| ExprKind::BitComplement(_)
53-
| ExprKind::Not(_) => (),
53+
| ExprKind::Not(_)
54+
| ExprKind::Call(_, _) => (),
5455
}
5556

5657
Err(ParseErrorKind::MustBeConstantInteger.at(expr.source))

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

+1
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,6 @@ pub fn translate_expr(
139139
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
140140
}))
141141
.at(expr.source),
142+
ExprKind::Call(_target, _args) => todo!("translate C call expression"),
142143
})
143144
}

Diff for: src/workspace/compile/normal.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ use crate::{
33
ast::AstFile,
44
compiler::Compiler,
55
data_units::ByteUnits,
6-
diagnostics::{ErrorDiagnostic, WarningDiagnostic},
6+
diagnostics::ErrorDiagnostic,
77
inflow::IntoInflow,
88
lexer::Lexer,
9-
line_column::Location,
109
parser::parse,
1110
show::{into_show, Show},
12-
source_files::Source,
1311
text::{IntoText, IntoTextStream},
1412
workspace::{
1513
fs::FsNodeId,
@@ -42,11 +40,6 @@ pub fn compile_normal_file(
4240
));
4341
}
4442
NormalFileKind::CSource => {
45-
compiler.diagnostics.push(WarningDiagnostic::new(
46-
"c source files are currently treated the same as headers",
47-
Source::new(key, Location { line: 1, column: 1 }),
48-
));
49-
5043
out_ast_files.push((
5144
normal_file.fs_node_id,
5245
c_code(compiler, text, key, CFileType::Source)?,

0 commit comments

Comments
 (0)