Skip to content

Commit

Permalink
Add a "closure" expression
Browse files Browse the repository at this point in the history
We had inlineClosureCall, but no specific expression for
closures.
  • Loading branch information
helje5 committed Oct 26, 2024
1 parent f163279 commit e354ce0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension CodeGenerator {

case .literal, .variableReference, .variablePath,
.keyPathLookup, .keyPath, .functionCall, .selfInit, .typeInit,
.cast, .inlineClosureCall:
.cast, .inlineClosureCall, .closure:
return string(for: expression)
}
}
Expand Down Expand Up @@ -103,6 +103,12 @@ public extension CodeGenerator {
case .forceUnwrap(let expression):
return "\(string(for: expression, wrapIfComplex: true))!"

case .closure(let statements):
return nestedGeneration {
indentedCodeBlock(addSpaceIfMissing: false) {
generateStatements(statements)
}
}
case .inlineClosureCall(let statements):
return nestedGeneration {
indentedCodeBlock(endSuffix: "()", addSpaceIfMissing: false) {
Expand Down Expand Up @@ -143,6 +149,8 @@ public extension CodeGenerator {
append(string(for: expression))
case .varargs: // indent better
append(string(for: expression))
case .closure: // indent better
append(string(for: expression))
case .inlineClosureCall: // indent better
append(string(for: expression))
case .functionCall(let call):
Expand Down
5 changes: 4 additions & 1 deletion Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public indirect enum Expression: Equatable {
/// `a!`
case forceUnwrap(Expression)

/// `{ return 46 + 2 }`
/// `{ return 46 + 2 }` (w/o calling it)
case closure([ Statement ])

/// `{ return 46 + 2 }()` (calling it)
case inlineClosureCall([ Statement ])
}

Expand Down

0 comments on commit e354ce0

Please sign in to comment.