Skip to content

Refactor nextValueForMacro so it does not rely on tail call optimization #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Sources/SWBMacro/MacroEvaluationScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,20 @@ final class MacroEvaluationContext {

/// Returns the next value for `macro` (this works correctly if even `macro` is the same as the macro declaration associated with the context, i.e. the inherited-value case — in this case the result is exactly the same as invoking `nextInheritedValue()`). Returns nil if there is no next value.
func nextValueForMacro(_ macro: MacroDeclaration) -> MacroValueAssignment? {
// If it’s the same macro as ours, it’s really just an inheritance lookup.
if macro === self.macro {
return nextInheritedValue()
}
// Otherwise, if we have a parent context, we defer the question to it.
if let parent = self.parent {
return parent.nextValueForMacro(macro)
var currentContext: MacroEvaluationContext = self
while true {
// If it’s the same macro as ours, it’s really just an inheritance lookup.
if macro === currentContext.macro {
return currentContext.nextInheritedValue()
}
// Otherwise, if we have a parent context, we defer the question to it.
if let parent = currentContext.parent {
currentContext = parent
} else {
// If we get this far, we need to look up in our scope’s table. We might or might not find it.
return currentContext.scope.table.lookupMacro(macro, overrideLookup: currentContext.lookup)?.firstMatchingCondition(currentContext.scope.conditionParameterValues)
}
}
// If we get this far, we need to look up in our scope’s table. We might or might not find it.
return scope.table.lookupMacro(macro, overrideLookup: lookup)?.firstMatchingCondition(scope.conditionParameterValues)
}
}

Expand Down