Skip to content

Commit

Permalink
fix number parser in cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Nov 2, 2023
1 parent d445c9c commit 8734795
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 3 additions & 7 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,24 +1776,20 @@ mod ops {
None => { cache.insert(key, None); }
}

fn parse_num(x: &str) -> Option<Number> {
x.parse::<f64>().ok().and_then(|x| Number::new(x).ok())
}

let res = match (a, b) {
(Value::Bool(a), Value::Bool(b)) => a.cmp(b).into(),
(Value::Bool(_), _) | (_, Value::Bool(_)) => None,

(Value::Number(a), Value::Number(b)) => a.cmp(b).into(),
(Value::String(a), Value::String(b)) => match parse_num(a).and_then(|a| parse_num(b).map(|b| (a, b))) {
(Value::String(a), Value::String(b)) => match Value::<C, S>::parse_number(a).and_then(|a| Value::<C, S>::parse_number(b).map(|b| (a, b))) {
Some((a, b)) => a.cmp(&b).into(),
None => UniCase::new(a.as_str()).cmp(&UniCase::new(b.as_str())).into(),
}
(Value::Number(a), Value::String(b)) => match parse_num(b) {
(Value::Number(a), Value::String(b)) => match Value::<C, S>::parse_number(b) {
Some(b) => a.cmp(&b).into(),
None => UniCase::new(&a.to_string()).cmp(&UniCase::new(b.as_ref())).into(),
}
(Value::String(a), Value::Number(b)) => match parse_num(a) {
(Value::String(a), Value::Number(b)) => match Value::<C, S>::parse_number(a) {
Some(a) => a.cmp(b).into(),
None => UniCase::new(a.as_ref()).cmp(&UniCase::new(&b.to_string())).into(),
}
Expand Down
21 changes: 11 additions & 10 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,20 +997,21 @@ impl<'gc, C: CustomTypes<S>, S: System<C>> Value<'gc, C, S> {
x => return Err(ConversionError { got: x.get_type(), expected: Type::Bool }),
})
}
pub(crate) fn parse_number(s: &str) -> Option<Number> {
let s = s.trim();
let parsed = match s.get(..2) {
Some("0x" | "0X") => i64::from_str_radix(&s[2..], 16).ok().map(|x| x as f64),
Some("0o" | "0O") => i64::from_str_radix(&s[2..], 8).ok().map(|x| x as f64),
Some("0b" | "0B") => i64::from_str_radix(&s[2..], 2).ok().map(|x| x as f64),
_ => s.parse::<f64>().ok(),
};
parsed.and_then(|x| Number::new(x).ok())
}
/// Attempts to interpret this value as a number.
pub fn as_number(&self) -> Result<Number, ConversionError<C, S>> {
match self {
Value::Number(x) => Ok(*x),
Value::String(x) => {
let x = x.trim();
let parsed = match x.get(..2) {
Some("0x" | "0X") => i64::from_str_radix(&x[2..], 16).ok().map(|x| x as f64),
Some("0o" | "0O") => i64::from_str_radix(&x[2..], 8).ok().map(|x| x as f64),
Some("0b" | "0B") => i64::from_str_radix(&x[2..], 2).ok().map(|x| x as f64),
_ => x.parse::<f64>().ok(),
};
parsed.and_then(|x| Number::new(x).ok()).ok_or(ConversionError { got: Type::String, expected: Type::Number })
}
Value::String(x) => Self::parse_number(x).ok_or(ConversionError { got: Type::String, expected: Type::Number }),
x => Err(ConversionError { got: x.get_type(), expected: Type::Number }),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/blocks/whitespace-in-numbers.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<blocks app="NetsBlox 2.1.4, http://netsblox.org" version="2.1.4"><block-definition collabId="item_-1_2" s="main" type="reporter" category="custom"><header></header><code></code><translations></translations><inputs></inputs><script><block collabId="item_1" s="doDeclareVariables"><list><l>a</l></list></block><block collabId="item_0" s="doSetVar"><l>a</l><block collabId="item_3" s="reportNewList"><list></list></block></block><block collabId="item_7" s="doAddToList"><block collabId="item_20" s="reportNewList"><list><block collabId="item_9" s="reportVariadicSum"><list><block collabId="item_10" s="reportJoinWords"><list><l> 12</l></list></block><l>0</l></list></block><block collabId="item_15_1" s="reportVariadicSum"><list><block collabId="item_15_3" s="reportJoinWords"><list><l>32 </l></list></block><l>0</l></list></block><block collabId="item_17_1" s="reportVariadicSum"><list><block collabId="item_17_3" s="reportJoinWords"><list><l> 14 </l></list></block><l>0</l></list></block></list></block><block collabId="item_8" var="a"/></block><block collabId="item_15" s="doAddToList"><block collabId="item_26" s="reportNewList"><list><block collabId="item_26_1" s="reportVariadicSum"><list><block collabId="item_26_6" s="reportJoinWords"><list><l> 0x12</l></list></block><l>0</l></list></block><block collabId="item_26_2" s="reportVariadicSum"><list><block collabId="item_26_5" s="reportJoinWords"><list><l>0x32 </l></list></block><l>0</l></list></block><block collabId="item_26_3" s="reportVariadicSum"><list><block collabId="item_26_4" s="reportJoinWords"><list><l> 0x14 </l></list></block><l>0</l></list></block></list></block><block collabId="item_15_2" var="a"/></block><block collabId="item_17" s="doAddToList"><block collabId="item_30" s="reportNewList"><list><block collabId="item_30_1" s="reportVariadicSum"><list><block collabId="item_30_6" s="reportJoinWords"><list><l> 0o12</l></list></block><l>0</l></list></block><block collabId="item_30_2" s="reportVariadicSum"><list><block collabId="item_30_5" s="reportJoinWords"><list><l>0o32 </l></list></block><l>0</l></list></block><block collabId="item_30_3" s="reportVariadicSum"><list><block collabId="item_30_4" s="reportJoinWords"><list><l> 0o14 </l></list></block><l>0</l></list></block></list></block><block collabId="item_17_2" var="a"/></block><block collabId="item_34" s="doAddToList"><block collabId="item_34_1" s="reportNewList"><list><block collabId="item_34_3" s="reportVariadicSum"><list><block collabId="item_34_8" s="reportJoinWords"><list><l> 0b1001</l></list></block><l>0</l></list></block><block collabId="item_34_4" s="reportVariadicSum"><list><block collabId="item_34_7" s="reportJoinWords"><list><l>0b1011 </l></list></block><l>0</l></list></block><block collabId="item_34_5" s="reportVariadicSum"><list><block collabId="item_34_6" s="reportJoinWords"><list><l> 0b11011 </l></list></block><l>0</l></list></block></list></block><block collabId="item_34_2" var="a"/></block><block collabId="item_4" s="doReport"><block collabId="item_6" var="a"/></block></script></block-definition></blocks>
<blocks app="NetsBlox 2.1.4, http://netsblox.org" version="2.1.4"><block-definition collabId="item_0_102" s="main" type="reporter" category="custom"><header></header><code></code><translations></translations><inputs></inputs><script><block collabId="item_0_96" s="doDeclareVariables"><list><l>a</l></list></block><block collabId="item_0_93" s="doSetVar"><l>a</l><block collabId="item_0_91" s="reportNewList"><list></list></block></block><block collabId="item_0_89" s="doAddToList"><block collabId="item_0_88" s="reportNewList"><list><block collabId="item_0_86" s="reportVariadicSum"><list><block collabId="item_0_84" s="reportJoinWords"><list><l> 12</l></list></block><l>0</l></list></block><block collabId="item_0_80" s="reportVariadicSum"><list><block collabId="item_0_78" s="reportJoinWords"><list><l>32 </l></list></block><l>0</l></list></block><block collabId="item_0_74" s="reportVariadicSum"><list><block collabId="item_0_72" s="reportJoinWords"><list><l> 14 </l></list></block><l>0</l></list></block></list></block><block collabId="item_0_68" var="a"/></block><block collabId="item_0_67" s="doAddToList"><block collabId="item_0_66" s="reportNewList"><list><block collabId="item_0_64" s="reportVariadicSum"><list><block collabId="item_0_62" s="reportJoinWords"><list><l> 0x12</l></list></block><l>0</l></list></block><block collabId="item_0_58" s="reportVariadicSum"><list><block collabId="item_0_56" s="reportJoinWords"><list><l>0x32 </l></list></block><l>0</l></list></block><block collabId="item_0_52" s="reportVariadicSum"><list><block collabId="item_0_50" s="reportJoinWords"><list><l> 0x14 </l></list></block><l>0</l></list></block></list></block><block collabId="item_0_46" var="a"/></block><block collabId="item_0_45" s="doAddToList"><block collabId="item_0_44" s="reportNewList"><list><block collabId="item_0_42" s="reportVariadicSum"><list><block collabId="item_0_40" s="reportJoinWords"><list><l> 0o12</l></list></block><l>0</l></list></block><block collabId="item_0_36" s="reportVariadicSum"><list><block collabId="item_0_34" s="reportJoinWords"><list><l>0o32 </l></list></block><l>0</l></list></block><block collabId="item_0_30" s="reportVariadicSum"><list><block collabId="item_0_28" s="reportJoinWords"><list><l> 0o14 </l></list></block><l>0</l></list></block></list></block><block collabId="item_0_24" var="a"/></block><block collabId="item_0_23" s="doAddToList"><block collabId="item_0_22" s="reportNewList"><list><block collabId="item_0_20" s="reportVariadicSum"><list><block collabId="item_0_18" s="reportJoinWords"><list><l> 0b1001</l></list></block><l>0</l></list></block><block collabId="item_0_14" s="reportVariadicSum"><list><block collabId="item_0_12" s="reportJoinWords"><list><l>0b1011 </l></list></block><l>0</l></list></block><block collabId="item_0_8" s="reportVariadicSum"><list><block collabId="item_0_6" s="reportJoinWords"><list><l> 0b11011 </l></list></block><l>0</l></list></block></list></block><block collabId="item_0_2" var="a"/></block><block collabId="item_38" s="doAddToList"><custom-block collabId="item_40" s="cmp %s %s"><l>12</l><l> 12</l></custom-block><block collabId="item_38_2" var="a"/></block><block collabId="item_43" s="doAddToList"><custom-block collabId="item_43_1" s="cmp %s %s"><l>0x7</l><l>0x10</l></custom-block><block collabId="item_43_2" var="a"/></block><block collabId="item_46" s="doAddToList"><custom-block collabId="item_46_1" s="cmp %s %s"><l> 0x7</l><l>0x10</l></custom-block><block collabId="item_46_2" var="a"/></block><block collabId="item_48" s="doAddToList"><custom-block collabId="item_48_1" s="cmp %s %s"><l>0x7</l><l>0x10 </l></custom-block><block collabId="item_48_2" var="a"/></block><block collabId="item_0_1" s="doReport"><block collabId="item_0" var="a"/></block></script></block-definition><block-definition collabId="item_4" s="cmp %&apos;a&apos; %&apos;b&apos;" type="reporter" category="custom"><header></header><code></code><translations></translations><inputs><input type="%s"></input><input type="%s"></input></inputs><script><block collabId="item_9" s="doReport"><block collabId="item_10" s="reportNewList"><list><block collabId="item_7" s="reportLessThan"><block collabId="item_24" var="a"/><block collabId="item_25" var="b"/></block><block collabId="item_12" s="reportLessThanOrEquals"><block collabId="item_26" var="a"/><block collabId="item_27" var="b"/></block><block collabId="item_13" s="reportEquals"><block collabId="item_28" var="a"/><block collabId="item_29" var="b"/></block><block collabId="item_16" s="reportNotEquals"><block collabId="item_30" var="a"/><block collabId="item_31" var="b"/></block><block collabId="item_18" s="reportGreaterThanOrEquals"><block collabId="item_32" var="a"/><block collabId="item_33" var="b"/></block><block collabId="item_20" s="reportGreaterThan"><block collabId="item_34" var="a"/><block collabId="item_35" var="b"/></block><block collabId="item_22" s="reportIsIdentical"><block collabId="item_36" var="a"/><block collabId="item_37" var="b"/></block></list></block></block></script></block-definition></blocks>
4 changes: 4 additions & 0 deletions src/test/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,10 @@ fn test_proc_whitespace_in_numbers() {
[18, 50, 20],
[10, 26, 12],
[9, 11, 27],
[false, true, true, false, true, false, false],
[true, true, false, true, false, false, false],
[true, true, false, true, false, false, false],
[true, true, false, true, false, false, false],
])).unwrap());
assert_values_eq(&res.unwrap().0.unwrap(), &expect, 1e-5, "ws in nums");
});
Expand Down

0 comments on commit 8734795

Please sign in to comment.