Skip to content

Commit 9b86075

Browse files
committed
Added support for basic C math operators
1 parent d5c15f3 commit 9b86075

File tree

1 file changed

+83
-22
lines changed

1 file changed

+83
-22
lines changed

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

+83-22
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use self::{
88
string::translate_expr_string,
99
};
1010
use crate::{
11-
ast::{self, AstFile, UnaryMathOperator, UnaryOperation, UnaryOperator},
11+
ast::{self, AstFile},
1212
c::parser::{
1313
error::ParseErrorKind,
14-
expr::{Expr, ExprKind},
14+
expr::{BinaryOperator, Expr, ExprKind},
1515
CTypedef, ParseError,
1616
},
1717
diagnostics::Diagnostics,
@@ -34,7 +34,62 @@ pub fn translate_expr(
3434
ExprKind::Nullptr => todo!(),
3535
ExprKind::Character(_, _) => todo!(),
3636
ExprKind::Compound(_) => todo!(),
37-
ExprKind::BinaryOperation(_) => todo!(),
37+
ExprKind::BinaryOperation(operation) => {
38+
let left = translate_expr(ast_file, typedefs, &operation.left, diagnostics)?;
39+
let right = translate_expr(ast_file, typedefs, &operation.right, diagnostics)?;
40+
41+
let op: ast::BinaryOperator = match operation.operator {
42+
BinaryOperator::LogicalOr => todo!(),
43+
BinaryOperator::LogicalAnd => todo!(),
44+
BinaryOperator::InclusiveOr => todo!(),
45+
BinaryOperator::ExclusiveOr => todo!(),
46+
BinaryOperator::BitwiseAnd => todo!(),
47+
BinaryOperator::Equals => todo!(),
48+
BinaryOperator::NotEquals => todo!(),
49+
BinaryOperator::LessThan => todo!(),
50+
BinaryOperator::GreaterThan => todo!(),
51+
BinaryOperator::LessThanEq => todo!(),
52+
BinaryOperator::GreaterThanEq => todo!(),
53+
BinaryOperator::LeftShift => todo!(),
54+
BinaryOperator::RightShift => todo!(),
55+
BinaryOperator::Add => ast::BasicBinaryOperator::Add.into(),
56+
BinaryOperator::Subtract => ast::BasicBinaryOperator::Subtract.into(),
57+
BinaryOperator::Multiply => ast::BasicBinaryOperator::Multiply.into(),
58+
BinaryOperator::Divide => ast::BasicBinaryOperator::Divide.into(),
59+
BinaryOperator::Modulus => ast::BasicBinaryOperator::Modulus.into(),
60+
BinaryOperator::Assign => todo!(),
61+
BinaryOperator::AddAssign => todo!(),
62+
BinaryOperator::SubtractAssign => todo!(),
63+
BinaryOperator::MultiplyAssign => todo!(),
64+
BinaryOperator::DivideAssign => todo!(),
65+
BinaryOperator::ModulusAssign => todo!(),
66+
BinaryOperator::LeftShiftAssign => todo!(),
67+
BinaryOperator::RightShiftAssign => todo!(),
68+
BinaryOperator::BitAndAssign => todo!(),
69+
BinaryOperator::BitXorAssign => todo!(),
70+
BinaryOperator::BitOrAssign => todo!(),
71+
};
72+
73+
match op {
74+
ast::BinaryOperator::Basic(operator) => {
75+
ast::ExprKind::BasicBinaryOperation(Box::new(ast::BasicBinaryOperation {
76+
operator,
77+
left,
78+
right,
79+
}))
80+
}
81+
ast::BinaryOperator::ShortCircuiting(operator) => {
82+
ast::ExprKind::ShortCircuitingBinaryOperation(Box::new(
83+
ast::ShortCircuitingBinaryOperation {
84+
operator,
85+
left,
86+
right,
87+
},
88+
))
89+
}
90+
}
91+
.at(expr.source)
92+
}
3893
ExprKind::Ternary(_) => todo!(),
3994
ExprKind::Cast(_) => todo!(),
4095
ExprKind::Subscript(_) => todo!(),
@@ -52,28 +107,34 @@ pub fn translate_expr(
52107
expr.source,
53108
diagnostics,
54109
)?,
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),
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),
110+
ExprKind::AddressOf(inner) => {
111+
ast::ExprKind::UnaryOperation(Box::new(ast::UnaryOperation {
112+
operator: ast::UnaryOperator::AddressOf,
113+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
114+
}))
115+
.at(expr.source)
116+
}
117+
ExprKind::Dereference(inner) => {
118+
ast::ExprKind::UnaryOperation(Box::new(ast::UnaryOperation {
119+
operator: ast::UnaryOperator::Dereference,
120+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
121+
}))
122+
.at(expr.source)
123+
}
124+
ExprKind::Negate(inner) => ast::ExprKind::UnaryOperation(Box::new(ast::UnaryOperation {
125+
operator: ast::UnaryOperator::Math(ast::UnaryMathOperator::Negate),
72126
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
73127
}))
74128
.at(expr.source),
75-
ExprKind::Not(inner) => ast::ExprKind::UnaryOperation(Box::new(UnaryOperation {
76-
operator: UnaryOperator::Math(UnaryMathOperator::Not),
129+
ExprKind::BitComplement(inner) => {
130+
ast::ExprKind::UnaryOperation(Box::new(ast::UnaryOperation {
131+
operator: ast::UnaryOperator::Math(ast::UnaryMathOperator::BitComplement),
132+
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
133+
}))
134+
.at(expr.source)
135+
}
136+
ExprKind::Not(inner) => ast::ExprKind::UnaryOperation(Box::new(ast::UnaryOperation {
137+
operator: ast::UnaryOperator::Math(ast::UnaryMathOperator::Not),
77138
inner: translate_expr(ast_file, typedefs, inner, diagnostics)?,
78139
}))
79140
.at(expr.source),

0 commit comments

Comments
 (0)