Skip to content

Commit

Permalink
Evaluate to false when #ifdef eval fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dannymcgee committed May 14, 2024
1 parent d705b25 commit 1064931
Showing 1 changed file with 56 additions and 54 deletions.
110 changes: 56 additions & 54 deletions packages/server/src/pre/pruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,69 +106,71 @@ impl Visitor for Pruner {
fn visit_branch(&mut self, branch: &Branch) -> FlowControl {
use DirectiveKind::*;

let eval = match branch.directive.kind() {
directive @ (IfDef | IfNDef | ElseIfDef | ElseIfNDef) => {
let symbol = match branch.condition.as_ref() {
Some(Expr::Ident(IdentExpr::Leaf(token))) => token.lexeme(),
Some(Expr::Primary(inner)) => match inner.expr.as_ref() {
Expr::Ident(IdentExpr::Leaf(token)) => token.lexeme(),
other => {
let eval = 'eval: {
match branch.directive.kind() {
directive @ (IfDef | IfNDef | ElseIfDef | ElseIfNDef) => {
let symbol = match branch.condition.as_ref() {
Some(Expr::Ident(IdentExpr::Leaf(token))) => token.lexeme(),
Some(Expr::Primary(inner)) => match inner.expr.as_ref() {
Expr::Ident(IdentExpr::Leaf(token)) => token.lexeme(),
other => {
self.errors.push(SpannedError {
message: format!("Expected identifier, found {other:?}"),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

break 'eval Value::Bool(false);
}
},
Some(other) => {
self.errors.push(SpannedError {
message: format!("Expected identifier, found {other:?}"),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

return FlowControl::Break;
break 'eval Value::Bool(false);
}
},
Some(other) => {
self.errors.push(SpannedError {
message: format!("Expected identifier, found {other:?}"),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

return FlowControl::Break;
}
None => {
self.errors.push(SpannedError {
message: "ifdef must be followed by an identifier".into(),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

return FlowControl::Break;
}
};
let is_defined = self.defs.contains_key(symbol.as_str());
None => {
self.errors.push(SpannedError {
message: "ifdef must be followed by an identifier".into(),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

break 'eval Value::Bool(false);
}
};
let is_defined = self.defs.contains_key(symbol.as_str());

match directive {
IfDef | ElseIfDef => Value::Bool(is_defined),
IfNDef | ElseIfNDef => Value::Bool(!is_defined),
_ => unreachable!(),
match directive {
IfDef | ElseIfDef => Value::Bool(is_defined),
IfNDef | ElseIfNDef => Value::Bool(!is_defined),
_ => unreachable!(),
}
}
Else => {
// If we've made it here, it's because the corresponding #if[n?def]
// evaluation was `false`
Value::Bool(true)
}
If | ElseIf => {
eprintln!(
" WARN: Skipping evaluation of expression: {:?}",
branch.condition
);
Value::Bool(false)
}
Endif => {
self.errors.push(SpannedError {
message: "Unexpected `#endif`".into(),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

return FlowControl::Break;
}
}
Else => {
// If we've made it here, it's because the corresponding #if[n?def]
// evaluation was `false`
Value::Bool(true)
}
If | ElseIf => {
eprintln!(
" WARN: Skipping evaluation of expression: {:?}",
branch.condition
);
Value::Bool(false)
}
Endif => {
self.errors.push(SpannedError {
message: "Unexpected `#endif`".into(),
source: self.source.clone(),
span: Some(branch.directive.span()),
});

return FlowControl::Break;
}
};

Expand Down

0 comments on commit 1064931

Please sign in to comment.