-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Direct ranges in fields e.g. foo: 0..255
#174
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,11 +176,19 @@ fn parse_type_choices( | |
} | ||
} | ||
|
||
fn type2_to_number_literal(type2: &Type2) -> isize { | ||
fn type2_to_number_literal(type2: &Type2) -> i128 { | ||
match type2 { | ||
Type2::UintValue { value, .. } => *value as isize, | ||
Type2::IntValue { value, .. } => *value, | ||
Type2::FloatValue { value, .. } => *value as isize, | ||
Type2::UintValue { value, .. } => *value as i128, | ||
Type2::IntValue { value, .. } => *value as i128, | ||
Type2::FloatValue { value, .. } => { | ||
// FloatToInt trait still experimental so just directly check | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this was more of something copy-pasted that went unnoticed during the float PR that someone made. I don't think we'd want silent converting to integers as the preferred behavior there. |
||
let as_int = *value as i128; | ||
assert_eq!( | ||
as_int as f64, *value, | ||
"decimal not supported. Issue: https://github.com/dcSpark/cddl-codegen/issues/178" | ||
); | ||
as_int | ||
} | ||
_ => panic!( | ||
"Value specified: {:?} must be a number literal to be used here", | ||
type2 | ||
|
@@ -216,23 +224,23 @@ fn parse_control_operator( | |
match operator.operator { | ||
RangeCtlOp::RangeOp { is_inclusive, .. } => { | ||
let range_start = match type2 { | ||
Type2::UintValue { value, .. } => *value as isize, | ||
Type2::IntValue { value, .. } => *value, | ||
Type2::FloatValue { value, .. } => *value as isize, | ||
Type2::UintValue { value, .. } => *value as i128, | ||
Type2::IntValue { value, .. } => *value as i128, | ||
Type2::FloatValue { value, .. } => *value as i128, | ||
_ => panic!("Number expected as range start. Found {:?}", type2), | ||
}; | ||
let range_end = match operator.type2 { | ||
Type2::UintValue { value, .. } => value as isize, | ||
Type2::IntValue { value, .. } => value, | ||
Type2::FloatValue { value, .. } => value as isize, | ||
Type2::UintValue { value, .. } => value as i128, | ||
Type2::IntValue { value, .. } => value as i128, | ||
Type2::FloatValue { value, .. } => value as i128, | ||
_ => unimplemented!("unsupported type in range control operator: {:?}", operator), | ||
}; | ||
ControlOperator::Range(( | ||
Some(range_start as i128), | ||
Some(range_start), | ||
Some(if is_inclusive { | ||
range_end as i128 | ||
range_end | ||
} else { | ||
(range_end + 1) as i128 | ||
range_end + 1 | ||
}), | ||
)) | ||
} | ||
|
@@ -247,31 +255,29 @@ fn parse_control_operator( | |
ControlOperator::CBOR(rust_type_from_type2(types, parent_visitor, &operator.type2)) | ||
} | ||
token::ControlOperator::EQ => ControlOperator::Range(( | ||
Some(type2_to_number_literal(&operator.type2) as i128), | ||
Some(type2_to_number_literal(&operator.type2) as i128), | ||
Some(type2_to_number_literal(&operator.type2)), | ||
Some(type2_to_number_literal(&operator.type2)), | ||
)), | ||
// TODO: this would be MUCH nicer (for error displaying, etc) to handle this in its own dedicated way | ||
// which might be necessary once we support other control operators anyway | ||
token::ControlOperator::NE => ControlOperator::Range(( | ||
Some((type2_to_number_literal(&operator.type2) + 1) as i128), | ||
Some((type2_to_number_literal(&operator.type2) - 1) as i128), | ||
Some(type2_to_number_literal(&operator.type2) + 1), | ||
Some(type2_to_number_literal(&operator.type2) - 1), | ||
)), | ||
token::ControlOperator::LE => ControlOperator::Range(( | ||
lower_bound, | ||
Some(type2_to_number_literal(&operator.type2) as i128), | ||
Some(type2_to_number_literal(&operator.type2)), | ||
)), | ||
token::ControlOperator::LT => ControlOperator::Range(( | ||
lower_bound, | ||
Some((type2_to_number_literal(&operator.type2) - 1) as i128), | ||
)), | ||
token::ControlOperator::GE => ControlOperator::Range(( | ||
Some(type2_to_number_literal(&operator.type2) as i128), | ||
None, | ||
)), | ||
token::ControlOperator::GT => ControlOperator::Range(( | ||
Some((type2_to_number_literal(&operator.type2) + 1) as i128), | ||
None, | ||
Some(type2_to_number_literal(&operator.type2) - 1), | ||
)), | ||
token::ControlOperator::GE => { | ||
ControlOperator::Range((Some(type2_to_number_literal(&operator.type2)), None)) | ||
} | ||
token::ControlOperator::GT => { | ||
ControlOperator::Range((Some(type2_to_number_literal(&operator.type2) + 1), None)) | ||
} | ||
token::ControlOperator::SIZE => { | ||
let base_range = match &operator.type2 { | ||
Type2::UintValue { value, .. } => { | ||
|
@@ -1121,7 +1127,7 @@ fn rust_type_from_type1( | |
)); | ||
ty.as_bytes() | ||
} | ||
Some(ControlOperator::Range(min_max)) => match &type1.type2 { | ||
Some(ControlOperator::Range((low, high))) => match &type1.type2 { | ||
Type2::Typename { ident, .. } | ||
if ident.to_string() == "uint" | ||
|| ident.to_string() == "int" | ||
|
@@ -1130,15 +1136,21 @@ fn rust_type_from_type1( | |
|| ident.to_string() == "float32" | ||
|| ident.to_string() == "float64" => | ||
{ | ||
match range_to_primitive(min_max.0, min_max.1) { | ||
match range_to_primitive(low, high) { | ||
Some(t) => t.into(), | ||
None => panic!( | ||
"unsupported range for {:?}: {:?}", | ||
"unsupported range for {:?}: {:?}. Issue: https://github.com/dcSpark/cddl-codegen/issues/173", | ||
ident.to_string().as_str(), | ||
control | ||
), | ||
} | ||
} | ||
}, | ||
// the base value will be a constant due to incomplete parsing earlier for explicit ranges | ||
// e.g. foo = 0..255 | ||
Type2::IntValue { .. } | | ||
Type2::UintValue { .. } => range_to_primitive(low, high) | ||
.map(Into::into) | ||
.expect("unsupported ranges. Only integer ranges mapping to primitives supported. Issue: https://github.com/dcSpark/cddl-codegen/issues/173"), | ||
_ => base_type, | ||
}, | ||
Some(ControlOperator::Default(default_value)) => base_type.default(default_value), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was to fix an issue noticed when I tried to add u64::MAX as a bound to something and it was rolling over to -1 silently.