Skip to content

Commit f44fb96

Browse files
committed
Added basic support for C logical, comparison, and bitwise operators
1 parent 9b86075 commit f44fb96

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

Diff for: src/ast/conforming.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CIntegerAssumptions;
22

3-
#[derive(Copy, Clone, Debug)]
3+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
44
pub enum Language {
55
Adept,
66
C,
@@ -19,4 +19,11 @@ impl ConformBehavior {
1919
ConformBehavior::C => Default::default(),
2020
}
2121
}
22+
23+
pub fn auto_c_integer_to_bool_conversion(&self) -> bool {
24+
match self {
25+
ConformBehavior::Adept(_) => false,
26+
ConformBehavior::C => true,
27+
}
28+
}
2229
}

Diff for: src/ast/expr/binary/short_circuiting.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::ast::Expr;
1+
use crate::ast::{Expr, Language};
22
use std::fmt::Display;
33

44
#[derive(Clone, Debug)]
55
pub struct ShortCircuitingBinaryOperation {
66
pub operator: ShortCircuitingBinaryOperator,
77
pub left: Expr,
88
pub right: Expr,
9+
pub language: Language,
910
}
1011

1112
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]

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

+16-15
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ pub fn translate_expr(
3838
let left = translate_expr(ast_file, typedefs, &operation.left, diagnostics)?;
3939
let right = translate_expr(ast_file, typedefs, &operation.right, diagnostics)?;
4040

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!(),
41+
let operator: ast::BinaryOperator = match operation.operator {
42+
BinaryOperator::LogicalOr => ast::ShortCircuitingBinaryOperator::Or.into(),
43+
BinaryOperator::LogicalAnd => ast::ShortCircuitingBinaryOperator::And.into(),
44+
BinaryOperator::InclusiveOr => ast::BasicBinaryOperator::BitwiseOr.into(),
45+
BinaryOperator::ExclusiveOr => ast::BasicBinaryOperator::BitwiseXor.into(),
46+
BinaryOperator::BitwiseAnd => ast::BasicBinaryOperator::BitwiseAnd.into(),
47+
BinaryOperator::Equals => ast::BasicBinaryOperator::Equals.into(),
48+
BinaryOperator::NotEquals => ast::BasicBinaryOperator::NotEquals.into(),
49+
BinaryOperator::LessThan => ast::BasicBinaryOperator::LessThan.into(),
50+
BinaryOperator::GreaterThan => ast::BasicBinaryOperator::GreaterThan.into(),
51+
BinaryOperator::LessThanEq => ast::BasicBinaryOperator::LessThanEq.into(),
52+
BinaryOperator::GreaterThanEq => ast::BasicBinaryOperator::GreaterThanEq.into(),
53+
BinaryOperator::LeftShift => ast::BasicBinaryOperator::LeftShift.into(),
54+
BinaryOperator::RightShift => ast::BasicBinaryOperator::RightShift.into(),
5555
BinaryOperator::Add => ast::BasicBinaryOperator::Add.into(),
5656
BinaryOperator::Subtract => ast::BasicBinaryOperator::Subtract.into(),
5757
BinaryOperator::Multiply => ast::BasicBinaryOperator::Multiply.into(),
@@ -70,7 +70,7 @@ pub fn translate_expr(
7070
BinaryOperator::BitOrAssign => todo!(),
7171
};
7272

73-
match op {
73+
match operator {
7474
ast::BinaryOperator::Basic(operator) => {
7575
ast::ExprKind::BasicBinaryOperation(Box::new(ast::BasicBinaryOperation {
7676
operator,
@@ -84,6 +84,7 @@ pub fn translate_expr(
8484
operator,
8585
left,
8686
right,
87+
language: ast::Language::C,
8788
},
8889
))
8990
}

Diff for: src/parser/parse_expr/primary/operator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{is_right_associative, is_terminating_token, Parser};
22
use crate::{
33
ast::{
4-
BasicBinaryOperation, BasicBinaryOperator, BinaryOperator, Expr, ExprKind,
4+
BasicBinaryOperation, BasicBinaryOperator, BinaryOperator, Expr, ExprKind, Language,
55
ShortCircuitingBinaryOperation, ShortCircuitingBinaryOperator,
66
},
77
inflow::Inflow,
@@ -79,6 +79,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
7979
operator: short_circuiting_operator,
8080
left: lhs,
8181
right: rhs,
82+
language: Language::Adept,
8283
}))
8384
}
8485
}

Diff for: src/resolve/conform/from_c_integer.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
Cast, CastFrom, Expr, ExprKind, IntegerSign, Type, TypeKind, TypedExpr, UnaryMathOperation,
55
UnaryMathOperator,
66
},
7-
ast::{CInteger, IntegerBits, OptionIntegerSignExt},
7+
ast::{CInteger, ConformBehavior, IntegerBits, OptionIntegerSignExt},
88
logic::implies,
99
source_files::Source,
1010
};
@@ -13,13 +13,14 @@ pub fn from_c_integer<O: Objective>(
1313
expr: &Expr,
1414
from_type: &Type,
1515
mode: ConformMode,
16+
behavior: ConformBehavior,
1617
from_c_integer: CInteger,
1718
from_sign: Option<IntegerSign>,
1819
to_type: &Type,
1920
source: Source,
2021
) -> ObjectiveResult<O> {
2122
match &to_type.kind {
22-
TypeKind::Boolean => from_c_integer_to_bool::<O>(expr, from_type, mode, source),
23+
TypeKind::Boolean => from_c_integer_to_bool::<O>(expr, from_type, mode, behavior, source),
2324
TypeKind::Integer(to_bits, to_sign) => from_c_integer_to_integer::<O>(
2425
expr,
2526
mode,
@@ -46,9 +47,10 @@ fn from_c_integer_to_bool<O: Objective>(
4647
expr: &Expr,
4748
from_type: &Type,
4849
mode: ConformMode,
50+
behavior: ConformBehavior,
4951
source: Source,
5052
) -> ObjectiveResult<O> {
51-
if !mode.allow_lossy_integer() {
53+
if !behavior.auto_c_integer_to_bool_conversion() && !mode.allow_lossy_integer() {
5254
return O::fail();
5355
}
5456

Diff for: src/resolve/conform/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub fn conform_expr<O: Objective>(
111111
&expr.expr,
112112
from_type,
113113
mode,
114+
behavior,
114115
*from_size,
115116
*from_sign,
116117
to_type,

Diff for: src/resolve/expr/short_circuiting_binary_operation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn resolve_short_circuiting_binary_operation_expr(
3131
&left,
3232
&local_bool_type,
3333
ConformMode::Normal,
34-
ctx.adept_conform_behavior(),
34+
ctx.conform_behavior(binary_operation.language),
3535
source,
3636
)
3737
.map_err(|_| {
@@ -59,7 +59,7 @@ pub fn resolve_short_circuiting_binary_operation_expr(
5959
&right,
6060
&local_bool_type,
6161
ConformMode::Normal,
62-
ctx.adept_conform_behavior(),
62+
ctx.conform_behavior(binary_operation.language),
6363
source,
6464
)
6565
.map_err(|_| {

0 commit comments

Comments
 (0)