diff --git a/docs/config/types.md b/docs/config/types.md index 707ee8fe..c751938e 100644 --- a/docs/config/types.md +++ b/docs/config/types.md @@ -44,10 +44,7 @@ Suppose we are trying to send the health of a player over the remote. We may def -Although, the type assertions will fail if 100 is passed. This is because **only the lower limit is inclusive**. To fix this, we must append a `=` to the end of the upper bound such as - - - +This works because limits are **inclusive**, and will include 0 and 100. ## Strings Unlike numbers, strings do not have a maximum length across different types. They can be any length unless they are constrained. diff --git a/zap/src/irgen/gen.rs b/zap/src/irgen/gen.rs index 62cbc399..f19b0166 100644 --- a/zap/src/irgen/gen.rs +++ b/zap/src/irgen/gen.rs @@ -228,15 +228,7 @@ fn range_check(stmts: &mut Vec, val: Expr, range: Range) { } if let Some(max) = range.max() { - assert( - stmts, - if range.max_inclusive() { - val.lte(max.into()) - } else { - val.lt(max.into()) - }, - None, - ); + assert(stmts, val.lte(max.into()), None); } } diff --git a/zap/src/parser/grammar.lalrpop b/zap/src/parser/grammar.lalrpop index d5a87520..f865d2b5 100644 --- a/zap/src/parser/grammar.lalrpop +++ b/zap/src/parser/grammar.lalrpop @@ -162,15 +162,12 @@ Ty: Ty = { Range: Range = { ".." => Range::default(), - ".." => Range::with_max(max, false), - "..=" => Range::with_max(max, true), + ".." => Range::new(None, Some(max)), - ".." => Range::with_min(min), + ".." => Range::new(Some(min), None), - ".." => Range::new(Some(min), Some(max), false), - "..=" => Range::new(Some(min), Some(max), true), - - => Range::new(Some(num), Some(num), true), + => Range::new(Some(num), Some(num)), + ".." => Range::new(Some(min), Some(max)), } Comma: Vec = { diff --git a/zap/src/util.rs b/zap/src/util.rs index 6f158e5e..5ecced6e 100644 --- a/zap/src/util.rs +++ b/zap/src/util.rs @@ -8,32 +8,11 @@ use crate::parser::Casing; pub struct Range { min: Option, max: Option, - max_inclusive: bool, } impl Range { - pub fn new(min: Option, max: Option, max_inclusive: bool) -> Self { - Self { - min, - max, - max_inclusive, - } - } - - pub fn with_min(min: T) -> Self { - Self { - min: Some(min), - max: None, - max_inclusive: false, - } - } - - pub fn with_max(max: T, max_inclusive: bool) -> Self { - Self { - min: None, - max: Some(max), - max_inclusive, - } + pub fn new(min: Option, max: Option) -> Self { + Self { min, max } } pub fn min(&self) -> Option { @@ -44,15 +23,10 @@ impl Range { self.max } - pub fn max_inclusive(&self) -> bool { - self.max_inclusive - } - pub fn cast(self) -> Range { Range { min: self.min.map(|x| NumCast::from(x).unwrap()), max: self.max.map(|x| NumCast::from(x).unwrap()), - max_inclusive: self.max_inclusive, } } } @@ -77,32 +51,16 @@ impl Range { impl Default for Range { fn default() -> Self { - Self { - min: None, - max: None, - max_inclusive: false, - } + Self { min: None, max: None } } } impl Display for Range { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match (self.min, self.max) { - (Some(min), Some(max)) => { - if self.max_inclusive { - write!(f, "{}..={}", min, max) - } else { - write!(f, "{}..{}", min, max) - } - } + (Some(min), Some(max)) => write!(f, "{}..{}", min, max), (Some(min), None) => write!(f, "{}..", min), - (None, Some(max)) => { - if self.max_inclusive { - write!(f, "..={}", max) - } else { - write!(f, "..{}", max) - } - } + (None, Some(max)) => write!(f, "..{}", max), (None, None) => write!(f, ".."), } }