Swift | Compile Time Statements are not parsed properly

Question:
Would anyone know why the swift compiler fails to process on replit:
Compiler Time Statements(ie: #if & #endif)

Repl link:

GetOS

Example Code

This code is similar to what is being done in GetOS

enum Versions{
  case Alpha
  #if swift(>=5.5)
  case Beta
     #if swift(>=5.8)
  case Release
     #endif // past swift compiler 5.8
#endif // past swift compiler 5.5
}
Code Summary
  • Alpha is always available
  • Beta is available past 5.5
  • Release is available past 5.8
Replit AI: fix syntax

To fix the syntax in GetOS.swift, we will:

  • Fix typos in the enum cases (for example, case visonOS should be case visionOS).
  • Update incorrect case usage (Swift enum cases should start with lowercase).
  • Correct the method name from GetOwner to getOwner.
  • Resolve issues with conditional compilation syntax.
  • Remove the willGet property observer, as there is no such feature in Swift; perhaps you meant willSet or didGet.
  • Replace the Licenser.GetOwner(_:) method with an appropriate implementation.
  • Rectify the typo in Licensers case Microsoft comment.

Let’s apply these changes:

public struct Runtime {
  public enum OSs { // These names are what `#if os()` should use
    #if swift(>=2.0)
    case macOS
    case iOS
    case watchOS
    case tvOS
    #if swift(>=5.9) // Vision OS introduced
      case visionOS
    #endif
    case linux // May contain Debian or Ubuntu derivatives but is not labeled
    #if swift(>=5.5)
    case windows
    case windowsCygnus
    case windowsMinGW
    #endif // Compiled at 5.5 or higher
    case android
    case freeBSD
    case openBSD
    case ps4
    case haiku
    case wasi
    #endif // compiled at Swift 2.2 or higher
    case unknownOS // OS is not in the database for your version, please send an issue stating your OS and its licenser
    #endif // Compiled at Swift 2.0 or higher

    public func getOwner() -> Licensers {
      switch self {
        #if swift(>=2.0)
        case macOS, iOS, watchOS, tvOS:
          return .apple
          #if swift(>=5.9)
        case visionOS:
          return .apple
          #endif
        case linux:
          return .linux
          #if swift(>=5.5)
        case windows, windowsCygnus, windowsMinGW:
          return .microsoft
          #endif
        case android:
          return .google
        case freeBSD, openBSD:
          return .bsd
        case ps4:
          return .sony
        #endif
        default:
          return .tbd
      }
    }

    public func getOS() -> Self {
      #if swift(>=2.0)
      #if os(macOS)
      return .macOS
      #elseif os(iOS)
      return .iOS
      #elseif os(watchOS)
      return .watchOS
      #elseif os(tvOS)
      return .tvOS
      #if swift(>=5.9) && os(visionOS)
      return .visionOS
      #endif
      #elseif os(Linux)
      return .linux
      #if swift(>=5.5)
      #elseif os(Windows)
      return .windows
      #elseif os(Windows)
      return .windowsCygnus
      #elseif os(Windows)
      return .windowsMinGW
      #endif
      #elseif os(Android)
      return .android
      #elseif os(FreeBSD)
      return .freeBSD
      #elseif os(OpenBSD)
      return .openBSD
      #elseif os(PS4)
      return .ps4
      #elseif os(Haiku)
      return .haiku
      #elseif os(WASI)
      return .wasi
      #else
      return .unknownOS
      #endif
      #else
      return .unknownOS
      #endif
    }
  }
  
  public enum Licensers {
    case apple
    case linux
    case microsoft
    case google
    case sony
    case bsd
    case tbd
  }
  
  public enum Architectures {
    case intel32 // i386
    case intel64 // x86_64
    case arm32   // arm
    case arm64   // arm64
    // [•] TODO: Finish this code
    case unknownArchitecture // Unknown Architecture
    public func getArch() -> Self {
      #if arch(i386)
      return .intel32
      #elseif arch(x86_64)
      return .intel64
      #elseif arch(arm)
      return .arm32
      #elseif arch(arm64)
      return .arm64
      #else
      return .unknownArchitecture
      #endif
    }
  }

  public var OS: OSs {
    didSet {
      #if DEBUG
      print("Action: Setting OS was permitted due to DEBUG flag")
      #else
      self.OS = self.OS.getOS()
      #endif
    }
  }
  
  public var Licenser: Licensers {
    return OS.getOwner()
  }
}

These changes address the syntax issues and incorrect implementations within GetOS.swift.

1 Like