Skip to content

Commit d5c15f3

Browse files
committed
Implemented dereference, negate, bit-complement, and not operators for C code
1 parent 3d47b70 commit d5c15f3

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ pub enum ExprKind {
4242
EnumConstant(String, Integer),
4343
CompoundLiteral(Box<CompoundLiteral>),
4444
AddressOf(Box<Expr>),
45+
Dereference(Box<Expr>),
46+
Negate(Box<Expr>),
47+
BitComplement(Box<Expr>),
48+
Not(Box<Expr>),
4549
}
4650

4751
impl ExprKind {
@@ -361,11 +365,23 @@ impl<'a> Parser<'a> {
361365
let inner = self.parse_expr_primary()?;
362366
return Ok(ExprKind::AddressOf(Box::new(inner)).at(source));
363367
}
364-
CTokenKind::Punctuator(Punctuator::Multiply) => todo!(),
368+
CTokenKind::Punctuator(Punctuator::Multiply) => {
369+
let inner = self.parse_expr_primary()?;
370+
return Ok(ExprKind::Dereference(Box::new(inner)).at(source));
371+
}
365372
CTokenKind::Punctuator(Punctuator::Add) => todo!(),
366-
CTokenKind::Punctuator(Punctuator::Subtract) => todo!(),
367-
CTokenKind::Punctuator(Punctuator::BitComplement) => todo!(),
368-
CTokenKind::Punctuator(Punctuator::Not) => todo!(),
373+
CTokenKind::Punctuator(Punctuator::Subtract) => {
374+
let inner = self.parse_expr_primary()?;
375+
return Ok(ExprKind::Negate(Box::new(inner)).at(source));
376+
}
377+
CTokenKind::Punctuator(Punctuator::BitComplement) => {
378+
let inner = self.parse_expr_primary()?;
379+
return Ok(ExprKind::BitComplement(Box::new(inner)).at(source));
380+
}
381+
CTokenKind::Punctuator(Punctuator::Not) => {
382+
let inner = self.parse_expr_primary()?;
383+
return Ok(ExprKind::Not(Box::new(inner)).at(source));
384+
}
369385
CTokenKind::Punctuator(Punctuator::Increment) => todo!(),
370386
CTokenKind::Punctuator(Punctuator::Decrement) => todo!(),
371387
CTokenKind::SizeofKeyword => todo!(),

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ pub fn evaluate_to_const_integer(expr: &Expr) -> Result<BigInt, ParseError> {
4646
| ExprKind::PostIncrement(_)
4747
| ExprKind::PostDecrement(_)
4848
| ExprKind::CompoundLiteral(_)
49-
| ExprKind::AddressOf(_) => (),
49+
| ExprKind::AddressOf(_)
50+
| ExprKind::Dereference(_)
51+
| ExprKind::Negate(_)
52+
| ExprKind::BitComplement(_)
53+
| ExprKind::Not(_) => (),
5054
}
5155

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

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

+21-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, UnaryOperation, UnaryOperator},
11+
ast::{self, AstFile, UnaryMathOperator, UnaryOperation, UnaryOperator},
1212
c::parser::{
1313
error::ParseErrorKind,
1414
expr::{Expr, ExprKind},
@@ -57,5 +57,25 @@ pub fn translate_expr(
5757
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
5858
}))
5959
.at(expr.source),
60+
ExprKind::Dereference(inner) => ast::ExprKind::UnaryOperation(Box::new(UnaryOperation {
61+
operator: UnaryOperator::Dereference,
62+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
63+
}))
64+
.at(expr.source),
65+
ExprKind::Negate(inner) => ast::ExprKind::UnaryOperation(Box::new(UnaryOperation {
66+
operator: UnaryOperator::Math(UnaryMathOperator::Negate),
67+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
68+
}))
69+
.at(expr.source),
70+
ExprKind::BitComplement(inner) => ast::ExprKind::UnaryOperation(Box::new(UnaryOperation {
71+
operator: UnaryOperator::Math(UnaryMathOperator::BitComplement),
72+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
73+
}))
74+
.at(expr.source),
75+
ExprKind::Not(inner) => ast::ExprKind::UnaryOperation(Box::new(UnaryOperation {
76+
operator: UnaryOperator::Math(UnaryMathOperator::Not),
77+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
78+
}))
79+
.at(expr.source),
6080
})
6181
}

0 commit comments

Comments
 (0)