Add "real"

This commit is contained in:
Tony Garnock-Jones 2021-08-08 17:08:32 -04:00
parent 61af114d5f
commit 965bda9f9e
7 changed files with 39 additions and 7 deletions

View File

@ -15,5 +15,6 @@ preserves-schema = { path = "../preserves-schema", version = "0.3.0" }
preserves = { path = "../preserves", version = "0.16.0" }
preserves-schema = { path = "../preserves-schema", version = "0.3.0" }
num = "0.4"
regex = "1.5"
thiserror = "1.0"

View File

@ -5,6 +5,9 @@ pub mod schemas;
pub use crate::schemas::path;
use num::bigint::BigInt;
use num::traits::cast::ToPrimitive;
use preserves::value::AtomClass;
use preserves::value::BinarySource;
use preserves::value::BytesBinarySource;
@ -299,6 +302,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::Kind { kind } => Ok(Node::new(KindStep {
kind: match &**kind {
path::ValueKind::Boolean => ValueClass::Atomic(AtomClass::Boolean),
@ -387,6 +391,26 @@ impl Step for TestStep {
delegate_finish_and_reset!(self, self.step);
}
#[derive(Debug)]
struct RealStep {
step: Node,
}
impl Step for RealStep {
fn accept(&mut self, path: Rc<Path>, value: &IOValue) {
match value.value() {
Value::SignedInteger(i) => if let Some(r) = BigInt::from(i).to_f64() {
self.step.accept(path, &IOValue::new(r))
},
Value::Float(f) => self.step.accept(path, &IOValue::new(f32::from(*f) as f64)),
Value::Double(_) => self.step.accept(path, value),
_ => (),
}
}
delegate_finish_and_reset!(self, self.step);
}
#[derive(Debug)]
struct VecCollector {
accumulator: Vec<IOValue>,
@ -535,9 +559,8 @@ fn parse_step(tokens: &[IOValue]) -> Result<Option<(path::Step, &[IOValue])>, Co
".embedded" => Ok(Some((path::Step::Axis(Box::new(path::Axis::Embedded)), remainder))),
"*" => Ok(Some((path::Step::Filter(Box::new(path::Filter::Nop)), remainder))),
"eq" => parse_comparison(remainder, path::Comparison::Eq),
"=" => parse_comparison(remainder, path::Comparison::Eq),
"ne" => parse_comparison(remainder, path::Comparison::Ne),
"eq" | "=" => parse_comparison(remainder, path::Comparison::Eq),
"ne" | "!=" => parse_comparison(remainder, path::Comparison::Ne),
"lt" => parse_comparison(remainder, path::Comparison::Lt),
"gt" => parse_comparison(remainder, path::Comparison::Gt),
"le" => parse_comparison(remainder, path::Comparison::Le),
@ -561,6 +584,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))),
"bool" => Ok(Some((path::Step::from(path::ValueKind::Boolean), remainder))),
"float" => Ok(Some((path::Step::from(path::ValueKind::Float), remainder))),
"double" => Ok(Some((path::Step::from(path::ValueKind::Double), remainder))),

View File

@ -14,7 +14,7 @@ gitlab = { repository = "preserves/preserves" }
[dependencies]
base64 = "0.13"
dtoa = "0.4"
num = "0.2"
num = "0.4"
regex = "1.5"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"

View File

@ -119,11 +119,11 @@ pub enum CompoundClass {
}
/// Single-precision IEEE 754 Value
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug)]
pub struct Float(pub f32);
/// Double-precision IEEE 754 Value
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug)]
pub struct Double(pub f64);
/// A Record `Value` -- INVARIANT: length always non-zero

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„„„„„„µ±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µ„„„„µ±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

@ -27,6 +27,7 @@ Filter =
/ <compare @op Comparison @literal any>
/ <regex @regex string>
/ <test @pred Predicate>
/ <real>
/ <kind @kind ValueKind>
.

View File

@ -66,6 +66,7 @@ Filters: narrow down a selection without moving
eq literal ;; Matches values (equal to/less than/greater than/etc.) the literal
= literal
ne literal
!= literal
lt literal
gt literal
le literal
@ -78,6 +79,10 @@ 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
;; Out-of-range ints (too big or too small) become various double infinities
;; Converting high-magnitude ints causes loss of precision
bool ;; Type filters
float
double