Skip to content
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

Implement CSS3 Calc #7185

Closed
wants to merge 20 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Clean up AST simplification code

  • Loading branch information
dzbarsky committed Sep 2, 2015
commit 2a976c0b2cc8176f55efa33b6c5ca65cb5c13774
@@ -379,7 +379,6 @@ pub mod specified {
#[derive(Clone, Debug)]
enum CalcAstNode {
Add(CalcSumNode),
Multiply(CalcProductNode),
Value(CalcValueNode),
}

@@ -417,7 +416,6 @@ pub mod specified {
}

let sum = CalcSumNode { products: products };
println!("Parsed sum {:?} ", sum);
Ok(sum)
}

@@ -450,7 +448,6 @@ pub mod specified {
}

let sum = CalcProductNode { values: values };
println!("Parsed product {:?} ", sum);
Ok(sum)
}

@@ -496,7 +493,6 @@ pub mod specified {
})),
Some(CalcAstNode::Value(ref value)) =>
Ok(CalcAstNode::Value(Calc::multiply_value(value, multiplier))),
_ => unreachable!()
}
}

@@ -525,14 +521,12 @@ pub mod specified {
let node = try!(Calc::simplify_product(node));
match node {
CalcAstNode::Value(value) => {
let product = CalcProductNode { values: vec!(value) };
if length == 1 {
return Ok(CalcAstNode::Multiply(product));
return Ok(CalcAstNode::Value(value));
}
simplified.push(product);
simplified.push(CalcProductNode { values: vec!(value) });
}
CalcAstNode::Add(sum) => simplified.push_all(&sum.products),
_ => return Err(())
}
}

@@ -551,6 +545,8 @@ pub mod specified {
let ast = try!(Calc::parse_sum(input));
let ast = try!(Calc::simplify_sum(ast));

println!("Simlified ast {:?} ", ast);

This comment has been minimized.

@pcwalton

pcwalton Aug 13, 2015

Contributor

(Remove this before committing)


let mut absolute = None;
let mut vw = None;
let mut vh = None;
@@ -561,42 +557,49 @@ pub mod specified {
let mut rem = None;
let mut percentage = None;

if let CalcAstNode::Add(ast) = ast {
for value in ast.products {
assert!(value.values.len() == 1);
match value.values[0] {
CalcValueNode::Percentage(p) =>
percentage = Some(percentage.unwrap_or(0.) + p),
CalcValueNode::Length(Length::Absolute(Au(au))) =>
absolute = Some(absolute.unwrap_or(0) + au),
CalcValueNode::Length(Length::ViewportPercentage(v)) =>
match v {
ViewportPercentageLength::Vw(val) =>
vw = Some(vw.unwrap_or(0.) + val),
ViewportPercentageLength::Vh(val) =>
vh = Some(vh.unwrap_or(0.) + val),
ViewportPercentageLength::Vmin(val) =>
vmin = Some(vmin.unwrap_or(0.) + val),
ViewportPercentageLength::Vmax(val) =>
vmax = Some(vmax.unwrap_or(0.) + val),
},
CalcValueNode::Length(Length::FontRelative(f)) =>
match f {
FontRelativeLength::Em(val) =>
em = Some(em.unwrap_or(0.) + val),
FontRelativeLength::Ex(val) =>
ex = Some(ex.unwrap_or(0.) + val),
FontRelativeLength::Rem(val) =>
rem = Some(rem.unwrap_or(0.) + val),
},
_ => return Err(())
}
let values = match ast {
CalcAstNode::Add(sum) => {
let mut values = Vec::new();
for product in sum.products {
assert!(product.values.len() == 1);
values.push(product.values[0].clone());
}
values
},
CalcAstNode::Value(value) => vec!(value)
};

for value in values {
match value {
CalcValueNode::Percentage(p) =>
percentage = Some(percentage.unwrap_or(0.) + p),
CalcValueNode::Length(Length::Absolute(Au(au))) =>
absolute = Some(absolute.unwrap_or(0) + au),
CalcValueNode::Length(Length::ViewportPercentage(v)) =>
match v {
ViewportPercentageLength::Vw(val) =>
vw = Some(vw.unwrap_or(0.) + val),
ViewportPercentageLength::Vh(val) =>
vh = Some(vh.unwrap_or(0.) + val),
ViewportPercentageLength::Vmin(val) =>
vmin = Some(vmin.unwrap_or(0.) + val),
ViewportPercentageLength::Vmax(val) =>
vmax = Some(vmax.unwrap_or(0.) + val),
},
CalcValueNode::Length(Length::FontRelative(f)) =>
match f {
FontRelativeLength::Em(val) =>
em = Some(em.unwrap_or(0.) + val),
FontRelativeLength::Ex(val) =>
ex = Some(ex.unwrap_or(0.) + val),
FontRelativeLength::Rem(val) =>
rem = Some(rem.unwrap_or(0.) + val),
},
_ => return Err(())
}
} else {
unreachable!()
}

Ok(Calc {
Ok(Calc {
absolute: absolute.map(Au),
vw: vw.map(ViewportPercentageLength::Vw),
vh: vh.map(ViewportPercentageLength::Vh),
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.