visitIfStatement method
Visit a SIfStatement.
Implementation
@override
Object? visitIfStatement(SIfStatement node) {
// Evaluate the expression
final expressionValue = node.condition!.accept<Object?>(this);
// If the evaluation returns an AsyncSuspensionRequest, return it immediately
if (expressionValue is AsyncSuspensionRequest) {
Logger.debug(
"[SIfStatement] Expression suspended (AsyncSuspensionRequest). Propagating.",
);
return expressionValue;
}
// Check if this is an if-case statement (pattern matching)
final caseClause = node.caseClause;
if (caseClause != null) {
// This is an if-case statement: if (expr case Pattern when guard) { ... }
// Pattern variables are bound in a scope of their own.
final caseEnvironment = Environment(enclosing: environment);
final matched = matchCaseClause(
caseClause,
expressionValue,
caseEnvironment,
);
if (matched is AsyncSuspensionRequest) return matched;
if (matched == true) {
final previousVisitorEnv = environment;
try {
environment = caseEnvironment; // Execute body in case scope
node.thenStatement!.accept<Object?>(this);
} finally {
environment = previousVisitorEnv;
}
} else if (node.elseStatement != null) {
// The pattern did not match or the guard failed.
node.elseStatement!.accept<Object?>(this);
}
return null;
}
// Regular boolean condition if statement
bool conditionResult;
final bridgedInstance = toBridgedInstance(expressionValue);
if (expressionValue is bool) {
conditionResult = expressionValue;
} else if (bridgedInstance.$2 && bridgedInstance.$1?.nativeObject is bool) {
conditionResult = bridgedInstance.$1!.nativeObject as bool;
} else {
throw RuntimeD4rtException(
"The condition of an 'if' must be a boolean, but was ${expressionValue?.runtimeType}.",
);
}
if (conditionResult) {
node.thenStatement!.accept<Object?>(this);
} else if (node.elseStatement != null) {
node.elseStatement!.accept<Object?>(this);
}
return null; // The If instruction normally ends with null
}