Rename real to ~real, and add ~int to match

This commit is contained in:
Tony Garnock-Jones 2021-08-08 17:19:58 -04:00
parent 965bda9f9e
commit 2ee1c48fcd
4 changed files with 32 additions and 3 deletions

View File

@ -7,6 +7,7 @@ pub use crate::schemas::path;
use num::bigint::BigInt;
use num::traits::cast::ToPrimitive;
use num::traits::cast::FromPrimitive;
use preserves::value::AtomClass;
use preserves::value::BinarySource;
@ -303,6 +304,7 @@ impl StepMaker for path::Filter {
path::Filter::Regex { regex } => Ok(Node::new(RegexStep { regex: regex::Regex::new(regex)?, step })),
path::Filter::Test { pred } => Ok(Node::new(TestStep { pred: compile_predicate(&**pred)?, step })),
path::Filter::Real => Ok(Node::new(RealStep { step })),
path::Filter::Int => Ok(Node::new(IntStep { step })),
path::Filter::Kind { kind } => Ok(Node::new(KindStep {
kind: match &**kind {
path::ValueKind::Boolean => ValueClass::Atomic(AtomClass::Boolean),
@ -411,6 +413,28 @@ impl Step for RealStep {
delegate_finish_and_reset!(self, self.step);
}
#[derive(Debug)]
struct IntStep {
step: Node,
}
impl Step for IntStep {
fn accept(&mut self, path: Rc<Path>, value: &IOValue) {
match value.value() {
Value::SignedInteger(_) => self.step.accept(path, value),
Value::Float(f) => if let Some(i) = BigInt::from_f32(f32::from(*f)) {
self.step.accept(path, &IOValue::new(i))
},
Value::Double(d) => if let Some(i) = BigInt::from_f64(f64::from(*d)) {
self.step.accept(path, &IOValue::new(i))
},
_ => (),
}
}
delegate_finish_and_reset!(self, self.step);
}
#[derive(Debug)]
struct VecCollector {
accumulator: Vec<IOValue>,
@ -584,7 +608,8 @@ fn parse_step(tokens: &[IOValue]) -> Result<Option<(path::Step, &[IOValue])>, Co
})), remainder)))
}
"real" => Ok(Some((path::Step::Filter(Box::new(path::Filter::Real)), remainder))),
"~real" => Ok(Some((path::Step::Filter(Box::new(path::Filter::Real)), remainder))),
"~int" => Ok(Some((path::Step::Filter(Box::new(path::Filter::Int)), remainder))),
"bool" => Ok(Some((path::Step::from(path::ValueKind::Boolean), remainder))),
"float" => Ok(Some((path::Step::from(path::ValueKind::Float), remainder))),

View File

@ -1,5 +1,5 @@
´³schema·³version³ definitions·³Axis´³orµµ±values´³rec´³lit³values„´³tupleµ„„„„µ± descendants´³rec´³lit³ descendants„´³tupleµ„„„„µ±at´³rec´³lit³at„´³tupleµ´³named³key³any„„„„„µ±label´³rec´³lit³label„´³tupleµ„„„„µ±keys´³rec´³lit³keys„´³tupleµ„„„„µ±length´³rec´³lit³length„´³tupleµ„„„„µ± annotations´³rec´³lit³ annotations„´³tupleµ„„„„µ±embedded´³rec´³lit³embedded„´³tupleµ„„„„„„³Step´³orµµ±Axis´³refµ„³Axis„„µ±Filter´³refµ„³Filter„„„„³Filter´³orµµ±nop´³rec´³lit³nop„´³tupleµ„„„„µ±compare´³rec´³lit³compare„´³tupleµ´³named³op´³refµ„³
Comparison„„´³named³literal³any„„„„„µ±regex´³rec´³lit³regex„´³tupleµ´³named³regex´³atom³String„„„„„„µ±test´³rec´³lit³test„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±real´³rec´³lit³real„´³tupleµ„„„„µ±kind´³rec´³lit³kind„´³tupleµ´³named³kind´³refµ„³ ValueKind„„„„„„„„³Selector´³seqof´³refµ„³Step„„³ Predicate´³orµµ±Selector´³refµ„³Selector„„µ±not´³rec´³lit³not„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±or´³rec´³lit³or„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„µ±and´³rec´³lit³and„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„„„³ ValueKind´³orµµ±Boolean´³lit³Boolean„„µ±Float´³lit³Float„„µ±Double´³lit³Double„„µ± SignedInteger´³lit³ SignedInteger„„µ±String´³lit³String„„µ±
Comparison„„´³named³literal³any„„„„„µ±regex´³rec´³lit³regex„´³tupleµ´³named³regex´³atom³String„„„„„„µ±test´³rec´³lit³test„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±real´³rec´³lit³real„´³tupleµ„„„„µ±int´³rec´³lit³int„´³tupleµ„„„„µ±kind´³rec´³lit³kind„´³tupleµ´³named³kind´³refµ„³ ValueKind„„„„„„„„³Selector´³seqof´³refµ„³Step„„³ Predicate´³orµµ±Selector´³refµ„³Selector„„µ±not´³rec´³lit³not„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±or´³rec´³lit³or„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„µ±and´³rec´³lit³and„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„„„³ ValueKind´³orµµ±Boolean´³lit³Boolean„„µ±Float´³lit³Float„„µ±Double´³lit³Double„„µ± SignedInteger´³lit³ SignedInteger„„µ±String´³lit³String„„µ±
ByteString´³lit³
ByteString„„µ±Symbol´³lit³Symbol„„µ±Record´³lit³Record„„µ±Sequence´³lit³Sequence„„µ±Set´³lit³Set„„µ±
Dictionary´³lit³

View File

@ -28,6 +28,7 @@ Filter =
/ <regex @regex string>
/ <test @pred Predicate>
/ <real>
/ <int>
/ <kind @kind ValueKind>
.

View File

@ -79,10 +79,13 @@ Filters: narrow down a selection without moving
^ literal ;; Matches a record having a the literal as its label -- equivalent to [.^ = literal]
real ;; Promotes int and float to double, passes on double unchanged, rejects others
~real ;; Promotes int and float to double, passes on double unchanged, rejects others
;; Out-of-range ints (too big or too small) become various double infinities
;; Converting high-magnitude ints causes loss of precision
~int ;; Converts float and double to closest integer, where possible
;; NaN and infinities are rejected
bool ;; Type filters
float
double