Skip to content

Commit 18c32b0

Browse files
committed
Added basic support for function calls in C
1 parent 57a5fe9 commit 18c32b0

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1222,11 +1222,13 @@ impl<'a> Parser<'a> {
12221222
}
12231223

12241224
let attributes = self.parse_attribute_specifier_sequence()?;
1225+
let expr = self.parse_expr_multiple()?;
12251226

1226-
return Ok(ExprStatement::Normal(
1227-
attributes,
1228-
self.parse_expr_multiple()?,
1229-
));
1227+
if !self.eat_punctuator(Punctuator::Semicolon) {
1228+
return Err(self.error("Expected ';' after statement"));
1229+
}
1230+
1231+
return Ok(ExprStatement::Normal(attributes, expr));
12301232
}
12311233

12321234
fn parse_label(&mut self) -> Result<Label, ParseError> {

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

+29-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use super::TranslateCtx;
1111
use crate::{
1212
ast,
1313
c::{
14+
self,
1415
ast::expr::{BinaryOperator, Expr, ExprKind},
15-
parser::{error::ParseErrorKind, ParseError},
16+
parser::ParseError,
1617
},
18+
name::Name,
1719
};
1820

1921
pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr, ParseError> {
@@ -91,7 +93,7 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
9193
ExprKind::PostIncrement(_) => todo!(),
9294
ExprKind::PostDecrement(_) => todo!(),
9395
ExprKind::Identifier(name) => {
94-
return Err(ParseErrorKind::UndefinedVariable(name.into()).at(expr.source));
96+
return Ok(ast::ExprKind::Variable(Name::plain(name)).at(expr.source));
9597
}
9698
ExprKind::EnumConstant(_, _) => todo!(),
9799
ExprKind::CompoundLiteral(compound_literal) => {
@@ -128,6 +130,30 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
128130
inner: translate_expr(ctx, inner)?,
129131
}))
130132
.at(expr.source),
131-
ExprKind::Call(_target, _args) => todo!("translate C call expression"),
133+
ExprKind::Call(target, c_args) => {
134+
eprintln!("warning: c function call expression are not isolated yet to only call functions defined within then same file");
135+
eprintln!("warning: c function call expression cannot call expression yet");
136+
137+
let c::ast::ExprKind::Identifier(target) = &target.as_ref().kind else {
138+
return Err(ParseError::message(
139+
"Calling the result of expressions is not supported yet",
140+
expr.source,
141+
));
142+
};
143+
144+
let args = c_args
145+
.iter()
146+
.map(|c_arg| translate_expr(ctx, c_arg))
147+
.collect::<Result<Vec<ast::Expr>, ParseError>>()?;
148+
149+
ast::ExprKind::Call(Box::new(ast::Call {
150+
name: Name::plain(target),
151+
args,
152+
expected_to_return: None,
153+
generics: vec![],
154+
using: vec![],
155+
}))
156+
.at(expr.source)
157+
}
132158
})
133159
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub fn declare_function(
114114
};
115115

116116
ctx.ast_file.funcs.push(Func { head, stmts });
117-
118117
Ok(())
119118
}
120119

0 commit comments

Comments
 (0)