Evaluate binary ops when inserting variables

This commit is contained in:
ConnorSkees 2020-02-29 20:09:41 -05:00
parent b62b9240c0
commit 7d39b0c86a
5 changed files with 12 additions and 12 deletions

View File

@ -181,7 +181,7 @@ impl AtRule {
let mut scope = scope.clone(); let mut scope = scope.clone();
if from < to { if from < to {
for i in from..(to + through) { for i in from..(to + through) {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None)); scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_unknown_atrule_body( stmts.extend(eat_unknown_atrule_body(
&mut body.clone().into_iter().peekable(), &mut body.clone().into_iter().peekable(),
&scope, &scope,
@ -190,7 +190,7 @@ impl AtRule {
} }
} else if from > to { } else if from > to {
for i in ((to - through)..(from + 1)).skip(1).rev() { for i in ((to - through)..(from + 1)).skip(1).rev() {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None)); scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_unknown_atrule_body( stmts.extend(eat_unknown_atrule_body(
&mut body.clone().into_iter().peekable(), &mut body.clone().into_iter().peekable(),
&scope, &scope,
@ -283,7 +283,7 @@ fn eat_unknown_atrule_body<I: Iterator<Item = Token>>(
})); }));
} }
Expr::VariableDecl(name, val) => { Expr::VariableDecl(name, val) => {
scope.insert_var(&name, *val); scope.insert_var(&name, *val)?;
} }
Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)), Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)),
} }

View File

@ -377,8 +377,8 @@ impl Scope {
} }
} }
pub fn insert_var(&mut self, s: &str, v: Value) -> Option<Value> { pub fn insert_var(&mut self, s: &str, v: Value) -> SassResult<Option<Value>> {
self.vars.insert(s.replace('_', "-"), v) Ok(self.vars.insert(s.replace('_', "-"), v.eval()?))
} }
pub fn var_exists(&self, v: &str) -> bool { pub fn var_exists(&self, v: &str) -> bool {

View File

@ -78,7 +78,7 @@ impl Function {
}, },
}, },
}; };
self.scope.insert_var(&arg.name, val); self.scope.insert_var(&arg.name, val)?;
} }
Ok(self) Ok(self)
} }

View File

@ -391,7 +391,7 @@ impl<'a> StyleSheetParser<'a> {
let VariableDecl { val, default } = let VariableDecl { val, default } =
eat_variable_value(&mut self.lexer, &self.global_scope)?; eat_variable_value(&mut self.lexer, &self.global_scope)?;
if !default || self.global_scope.get_var(&name).is_err() { if !default || self.global_scope.get_var(&name).is_err() {
self.global_scope.insert_var(&name, val); self.global_scope.insert_var(&name, val)?;
} }
} }
TokenKind::MultilineComment(_) => { TokenKind::MultilineComment(_) => {
@ -524,10 +524,10 @@ impl<'a> StyleSheetParser<'a> {
} }
Expr::VariableDecl(name, val) => { Expr::VariableDecl(name, val) => {
if self.scope == 0 { if self.scope == 0 {
scope.insert_var(&name, *val.clone()); scope.insert_var(&name, *val.clone())?;
self.global_scope.insert_var(&name, *val); self.global_scope.insert_var(&name, *val)?;
} else { } else {
scope.insert_var(&name, *val); scope.insert_var(&name, *val)?;
} }
} }
Expr::Include(rules) => stmts.extend(rules), Expr::Include(rules) => stmts.extend(rules),

View File

@ -81,7 +81,7 @@ impl Mixin {
}, },
}, },
}; };
self.scope.insert_var(&arg.name, val); self.scope.insert_var(&arg.name, val)?;
} }
Ok(self) Ok(self)
} }
@ -114,7 +114,7 @@ impl Mixin {
})); }));
} }
Expr::VariableDecl(name, val) => { Expr::VariableDecl(name, val) => {
self.scope.insert_var(&name, *val); self.scope.insert_var(&name, *val)?;
} }
Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)), Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)),
} }