Calls to undefined functions should be treated as idents

This commit is contained in:
ConnorSkees 2020-02-09 20:26:14 -05:00
parent e37179cfd7
commit 4c70b84ed7
5 changed files with 54 additions and 6 deletions

View File

@ -76,7 +76,7 @@ impl Display for Symbol {
Self::At => write!(f, "@"),
Self::Dollar => write!(f, "$"),
Self::OpenParen => write!(f, "("),
Self::CloseParen => write!(f, "),"),
Self::CloseParen => write!(f, ")"),
Self::OpenCurlyBrace => write!(f, "{{"),
Self::CloseCurlyBrace => write!(f, "}}"),
Self::OpenSquareBrace => write!(f, "["),

View File

@ -81,7 +81,7 @@ impl Display for Number {
let mut frac = self.val.fract();
if frac != BigRational::from_integer(BigInt::from(0)) {
f.write_char('.')?;
for _ in 0..(PRECISION-1) {
for _ in 0..(PRECISION - 1) {
frac *= BigRational::from_integer(BigInt::from(10));
write!(f, "{}", frac.to_integer())?;
frac = frac.fract();

View File

@ -22,6 +22,13 @@ impl Add for Value {
},
Self::Dimension(num, unit) => match other {
Self::Dimension(num2, unit2) => Value::Dimension(num + num2, unit),
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}", num, unit, s), quotes)
}
_ => todo!(),
},
// Self::List(..) => todo!(),

View File

@ -219,15 +219,47 @@ impl Value {
..
}) => {
toks.next();
let args = eat_call_args(toks, scope);
let func = match scope.get_fn(&s) {
Ok(f) => f,
Err(_) => match GLOBAL_FUNCTIONS.get(&s) {
Some(f) => return f(&args, scope),
None => todo!("called undefined function"),
Some(f) => return f(&eat_call_args(toks, scope), scope),
None => {
s.push('(');
let mut unclosed_parens = 0;
while let Some(t) = toks.next() {
match &t.kind {
TokenKind::Symbol(Symbol::OpenParen) => {
unclosed_parens += 1;
}
TokenKind::Interpolation => s.push_str(
&parse_interpolation(toks, scope)
.iter()
.map(|x| x.kind.to_string())
.collect::<String>(),
),
TokenKind::Variable(v) => s.push_str(
&scope
.get_var(v)
.expect("expected variable to exist")
.to_string(),
),
TokenKind::Symbol(Symbol::CloseParen) => {
if unclosed_parens <= 1 {
s.push(')');
break;
} else {
unclosed_parens -= 1;
}
}
_ => {}
}
s.push_str(&t.kind.to_string());
}
return Some(Value::Ident(s, QuoteKind::None));
}
},
};
Some(func.clone().args(&args).call())
Some(func.clone().args(&eat_call_args(toks, scope)).call())
}
_ => {
if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) {

View File

@ -294,3 +294,12 @@ test!(
"a {\n color: 'foo' - 1px;\n}\n",
"a {\n color: \"foo\"-1px;\n}\n"
);
test!(
undefined_function_call_is_ident,
"a {\n color: foo();\n}\n"
);
test!(
undefined_function_call_is_ident_adds,
"a {\n color: 1 + foo();\n}\n",
"a {\n color: 1foo();\n}\n"
);