refactor calculation of default variables

This commit is contained in:
Connor Skees 2020-08-18 00:17:06 -04:00
parent 8d1e8a99c5
commit 00a7659e69
3 changed files with 76 additions and 8 deletions

View File

@ -32,6 +32,7 @@ impl<'a> Parser<'a> {
self.whitespace_or_comment(); self.whitespace_or_comment();
if init_cond.is_true() { if init_cond.is_true() {
self.scopes.enter_new_scope();
found_true = true; found_true = true;
body = Parser { body = Parser {
toks: self.toks, toks: self.toks,
@ -52,6 +53,7 @@ impl<'a> Parser<'a> {
module_config: self.module_config, module_config: self.module_config,
} }
.parse_stmt()?; .parse_stmt()?;
self.scopes.exit_scope();
} else { } else {
self.throw_away_until_closing_curly_brace()?; self.throw_away_until_closing_curly_brace()?;
} }
@ -89,6 +91,7 @@ impl<'a> Parser<'a> {
}; };
if cond { if cond {
found_true = true; found_true = true;
self.scopes.enter_new_scope();
body = Parser { body = Parser {
toks: self.toks, toks: self.toks,
map: self.map, map: self.map,
@ -108,6 +111,7 @@ impl<'a> Parser<'a> {
module_config: self.module_config, module_config: self.module_config,
} }
.parse_stmt()?; .parse_stmt()?;
self.scopes.exit_scope();
} else { } else {
self.throw_away_until_closing_curly_brace()?; self.throw_away_until_closing_curly_brace()?;
} }
@ -119,7 +123,8 @@ impl<'a> Parser<'a> {
self.throw_away_until_closing_curly_brace()?; self.throw_away_until_closing_curly_brace()?;
break; break;
} else { } else {
return Parser { self.scopes.enter_new_scope();
let tmp = Parser {
toks: self.toks, toks: self.toks,
map: self.map, map: self.map,
path: self.path, path: self.path,
@ -138,6 +143,8 @@ impl<'a> Parser<'a> {
module_config: self.module_config, module_config: self.module_config,
} }
.parse_stmt(); .parse_stmt();
self.scopes.exit_scope();
return tmp;
} }
} }
_ => { _ => {

View File

@ -55,25 +55,35 @@ impl<'a> Parser<'a> {
} }
self.parse_value_from_vec(val_toks, true)?.node self.parse_value_from_vec(val_toks, true)?.node
} else if !self.at_root { } else if self.at_root {
self.parse_value_from_vec(val_toks, true)?.node
} else {
if self.scopes.default_var_exists(ident) { if self.scopes.default_var_exists(ident) {
return Ok(()); return Ok(());
} }
self.parse_value_from_vec(val_toks, true)?.node
} else {
self.parse_value_from_vec(val_toks, true)?.node self.parse_value_from_vec(val_toks, true)?.node
}; };
if self.at_root && !self.flags.in_control_flow() { if self.at_root && self.global_scope.var_exists(ident) {
self.global_scope.insert_var(ident, value); if !self.global_scope.default_var_exists(ident) {
return Ok(()); self.global_scope.insert_var(ident, value.clone());
}
} else if self.at_root
&& !self.flags.in_control_flow()
&& !self.global_scope.default_var_exists(ident)
{
self.global_scope.insert_var(ident, value.clone());
} }
if global { if global {
self.global_scope.insert_var(ident, value.clone()); self.global_scope.insert_var(ident, value.clone());
} }
if self.at_root && !self.flags.in_control_flow() {
return Ok(());
}
self.scopes.insert_var(ident, value); self.scopes.insert_var(ident, value);
return Ok(()); return Ok(());

View File

@ -156,7 +156,7 @@ test!(
default_var_overrides_when_null_declared_global, default_var_overrides_when_null_declared_global,
"$a: null; "$a: null;
$a: red !default; $a: red !default;
a { a {
color: $a; color: $a;
}", }",
@ -208,6 +208,57 @@ test!(
}", }",
"a {\n color: outer;\n}\n" "a {\n color: outer;\n}\n"
); );
test!(
variable_declared_at_root_inside_if,
"@if true {
$a: outer;
}
a {
color: variable-exists(a);
}",
"a {\n color: false;\n}\n"
);
test!(
variable_declared_at_root_inside_if_default,
"@if true {
$a: outer !default;
}
a {
color: variable-exists(a);
}",
"a {\n color: false;\n}\n"
);
test!(
variable_declared_at_root_inside_if_global,
"@if true {
$a: outer !global;
}
a {
color: variable-exists(a);
}",
"a {\n color: true;\n}\n"
);
test!(
variable_declared_at_root_and_globally_inside_if_default,
"$a: null;
@if true {
$a: null;
$a: outer !default;
a {
color: $a;
}
}
a {
color: $a;
}",
"a {\n color: outer;\n}\n\na {\n color: outer;\n}\n"
);
// https://github.com/Kixiron/lasso/issues/7 // https://github.com/Kixiron/lasso/issues/7
test!( test!(
regression_test_for_lasso_0_3_0, regression_test_for_lasso_0_3_0,