Skip to content

Commit ec80728

Browse files
committed
Added support for C address-of expressions
1 parent 6f3544c commit ec80728

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum ExprKind {
4141
Identifier(String),
4242
EnumConstant(String, Integer),
4343
CompoundLiteral(Box<CompoundLiteral>),
44+
AddressOf(Box<Expr>),
4445
}
4546

4647
impl ExprKind {
@@ -353,8 +354,13 @@ impl<'a> Parser<'a> {
353354
pub fn parse_expr_primary_base(&mut self) -> Result<Expr, ParseError> {
354355
// Parse sequence of unary operators and casts
355356

357+
let source = self.input.peek().source;
358+
356359
match &self.input.peek().kind {
357-
CTokenKind::Punctuator(Punctuator::Ampersand) => todo!(),
360+
CTokenKind::Punctuator(Punctuator::Ampersand) => {
361+
let inner = self.parse_expr_primary()?;
362+
return Ok(ExprKind::AddressOf(Box::new(inner)).at(source));
363+
}
358364
CTokenKind::Punctuator(Punctuator::Multiply) => todo!(),
359365
CTokenKind::Punctuator(Punctuator::Add) => todo!(),
360366
CTokenKind::Punctuator(Punctuator::Subtract) => todo!(),

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ pub fn evaluate_to_const_integer(expr: &Expr) -> Result<BigInt, ParseError> {
4545
| ExprKind::Compound(_)
4646
| ExprKind::PostIncrement(_)
4747
| ExprKind::PostDecrement(_)
48-
| ExprKind::CompoundLiteral(_) => (),
48+
| ExprKind::CompoundLiteral(_)
49+
| ExprKind::AddressOf(_) => (),
4950
}
5051

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

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use self::{
88
string::translate_expr_string,
99
};
1010
use crate::{
11-
ast::{self, AstFile},
11+
ast::{self, AstFile, UnaryOperation, UnaryOperator},
1212
c::parser::{
1313
error::ParseErrorKind,
1414
expr::{Expr, ExprKind},
@@ -52,5 +52,10 @@ pub fn translate_expr(
5252
expr.source,
5353
diagnostics,
5454
)?,
55+
ExprKind::AddressOf(inner) => ast::ExprKind::UnaryOperation(Box::new(UnaryOperation {
56+
operator: UnaryOperator::AddressOf,
57+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
58+
}))
59+
.at(expr.source),
5560
})
5661
}

Diff for: src/resolve/polymorph/resolver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a> PolyRecipeResolver<'a> {
4444

4545
pub fn resolve_type(&self, ty: &asg::Type) -> Result<asg::Type, PolymorphError> {
4646
Ok(match &ty.kind {
47-
asg::TypeKind::Unresolved => panic!(),
47+
asg::TypeKind::Unresolved => panic!("unresolved type"),
4848
asg::TypeKind::Boolean
4949
| asg::TypeKind::Integer(_, _)
5050
| asg::TypeKind::CInteger(_, _)

0 commit comments

Comments
 (0)