Skip to content

Commit

Permalink
SwiftFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
LiarPrincess committed Nov 10, 2020
1 parent 3794f3d commit f1db357
Show file tree
Hide file tree
Showing 63 changed files with 609 additions and 431 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0
127 changes: 127 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# =============
# == Options ==
# =============

# Use 'swiftformat --options' to list all of the possible options
# For most of the options we use default value (except for '--self')

--allman false
--commas always
--elseposition same-line # I hate it, but this is 'community choice' in Swift
--empty void
--header ignore # We don't have a header. Headers are ugly.
--ifdef indent
--indent 2
--indentcase false
--linebreaks lf
--maxwidth none # 80 is preferred (but not required), don't go over 100
--nospaceoperators ..<, ...
--operatorfunc spaced
--self insert # Yes!
--selfrequired
--semicolons inline
--stripunusedargs closure-only
--tabwidth unspecified # Don't use tabs!
--trailingclosures
--trimwhitespace always
--xcodeindentation disabled

# Arguments are
# - group - group size (count % group == 0)
# - threshold - min digit count to apply (number.count >= threshold)
--decimalgrouping 3,1
--binarygrouping 4,1
--octalgrouping 4,1
--hexgrouping 4,1
--exponentcase lowercase
--exponentgrouping enabled
--fractiongrouping enabled
--hexliteralcase lowercase

--wraparguments preserve
--wrapcollections preserve
--wrapparameters preserve
--closingparen balanced

# We have our own (see: './Scipts/sort_swift_imports')
# But it is not required to follow it.
--importgrouping alphabetized

# Our own rule: all patterns should start at the same column,
# otherwise reader has to 'zig-zag' to read which pattern is bound where.
# (in other words: if all patterns have binding -> hoist, otherwise per variable)
#
# For example, this is not allowed (or at least frowned upon):
# switch frozen {
# case .elsa
# case let .anna(x) <-- nope, both 'elsa' and 'anna' should start the same column
# }
--patternlet hoist

# ===========
# == Rules ==
# ===========

--disable andOperator # Use '&&' ot ',' wherever you want
--enable anyObjectProtocol
--enable blankLinesAroundMark
--enable blankLinesAtEndOfScope
--disable blankLinesAtStartOfScope # Empty line after 'class' definition
--enable blankLinesBetweenScopes
--enable braces
--enable consecutiveBlankLines
--enable consecutiveSpaces
--enable duplicateImports
--enable elseOnSameLine
--enable emptyBraces
--enable fileHeader
--disable hoistPatternLet
--disable indent # This breaks a lof of already written stuff.
--disable isEmpty # False-positives
--enable leadingDelimiters
--enable linebreakAtEndOfFile
--enable linebreaks
--enable numberFormatting
# --enable ranges - deprecated
--enable redundantBackticks
--enable redundantBreak
--enable redundantExtensionACL
--enable redundantFileprivate
--enable redundantGet
--enable redundantInit
--enable redundantLet
--enable redundantLetError
--enable redundantNilInit
--enable redundantObjc
--enable redundantParens
--enable redundantPattern
--disable redundantRawValues # It is 'ok' to specify raw value (even if it is the same as value)
--disable redundantReturn # implicit = bad
--enable redundantSelf
--enable redundantVoidReturnType
--enable semicolons
--disable sortedImports
--enable spaceAroundBraces
--enable spaceAroundBrackets
--enable spaceAroundComments
--enable spaceAroundGenerics
--enable spaceAroundOperators
--enable spaceAroundParens
--enable spaceInsideBraces
--enable spaceInsideBrackets
--enable spaceInsideComments
--enable spaceInsideGenerics
--enable spaceInsideParens
--disable specifiers # Conflict with SwiftLint
--enable strongOutlets
--enable strongifiedSelf
--enable todos
--enable trailingClosures
--disable trailingCommas #
--enable trailingSpace
--enable typeSugar
--enable unusedArguments
--enable void
--disable wrap # Breaks already written code
--disable wrapArguments # See above
--disable yodaConditions # '0 < x && x < 10' is better than ' x > 0 && x < 10'
50 changes: 25 additions & 25 deletions Sources/Code generation/Output/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
func getEnumCase(_ opcode: Opcode) -> String {
var result = opcode.mnemonic.lowercased()
if result == "prefix" { return result }
if result == "stop" { return result }
if result == "stop" { return result }

if let operand1 = opcode.operand1 {
result += "_"
Expand Down Expand Up @@ -61,24 +61,24 @@ private func getOperandValue(_ value: String) -> String {
case "30H": return "30"
case "38H": return "38"

case "SP": return "sp"
case "SP": return "sp"
case "SP+r8": return "spR8"

case "d8": return "d8"
case "d8": return "d8"
case "d16": return "d16"
case "r8": return "r8"
case "r8": return "r8"
case "a16": return "a16"
case "NC": return "nc"
case "NZ": return "nz"
case "Z": return "z"

case "(BC)": return "pBC"
case "(C)": return "pC"
case "(DE)": return "pDE"
case "(HL)": return "pHL"
case "NC": return "nc"
case "NZ": return "nz"
case "Z": return "z"

case "(BC)": return "pBC"
case "(C)": return "pC"
case "(DE)": return "pDE"
case "(HL)": return "pHL"
case "(HL+)": return "pHLI"
case "(HL-)": return "pHLD"
case "(a8)": return "pA8"
case "(a8)": return "pA8"
case "(a16)": return "pA16"

default: return "Unknown value: " + value.lowercased()
Expand Down Expand Up @@ -154,7 +154,7 @@ func getUnprefixedOpcodeCall(_ opcode: Opcode) -> String {
let operand1 = opcode.operand1!.lowercased()
let operand2 = opcode.operand2!.lowercased()

if isA(operand1) && isRegister(operand2) { return "add_a_r(.\(operand2))" }
if isA(operand1) && isRegister(operand2) { return "add_a_r(.\(operand2))" }
if isHL(operand1) && isCombinedRegister(operand2) { return "add_hl_r(.\(operand2))" }
}

Expand Down Expand Up @@ -252,9 +252,9 @@ func getUnprefixedOpcodeCall(_ opcode: Opcode) -> String {
}

case "rlca": return "rlca()"
case "rla": return "rla()"
case "rla": return "rla()"
case "rrca": return "rrca()"
case "rra": return "rra()"
case "rra": return "rra()"

case "jp":
switch opcode.addr {
Expand Down Expand Up @@ -309,15 +309,15 @@ func getUnprefixedOpcodeCall(_ opcode: Opcode) -> String {
let argument = "0x" + operand.dropLast()
return "rst(\(argument))"

case "stop": return "stop()"
case "daa": return "daa()"
case "cpl": return "cpl()"
case "scf": return "scf()"
case "ccf": return "ccf()"
case "halt": return "halt()"
case "stop": return "stop()"
case "daa": return "daa()"
case "cpl": return "cpl()"
case "scf": return "scf()"
case "ccf": return "ccf()"
case "halt": return "halt()"
case "prefix": return "executePrefixed(\(next8))"
case "di": return "di()"
case "ei": return "ei()"
case "di": return "di()"
case "ei": return "ei()"

default: break
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func getCBPrefixedOpcodeCall(_ opcode: Opcode) -> String {
return ""
}

private let singleRegisters = ["a", "b", "c", "d", "e", "h", "l"]
private let singleRegisters = ["a", "b", "c", "d", "e", "h", "l"]
private let combinedRegisters = ["af", "bc", "de", "hl"]
private let jumpConditions = ["nz", "z", "nc", "c"]

Expand Down
4 changes: 2 additions & 2 deletions Sources/Code generation/Output/PrintOpcodeEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private func printOpcodeEnum(_ enumName: String, _ opcodes: [Opcode]) {
print("")
}

private func byAddress(_ opcodes: [Opcode]) -> [String:Opcode] {
var result = [String:Opcode]()
private func byAddress(_ opcodes: [Opcode]) -> [String: Opcode] {
var result = [String: Opcode]()
for op in opcodes {
result[op.addr] = op
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Code generation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation

let currentFile = URL(fileURLWithPath: #file)
let sourcesDir = currentFile.deletingLastPathComponent().deletingLastPathComponent()
let cpuDir = sourcesDir.appendingPathComponent("GameBoyKit").appendingPathComponent("Cpu")
let cpuDir = sourcesDir.appendingPathComponent("GameBoyKit").appendingPathComponent("Cpu")
let debugDir = sourcesDir.appendingPathComponent("GameBoyKit").appendingPathComponent("Debug")

let opcodes = try readOpcodes()
Expand Down Expand Up @@ -34,7 +34,7 @@ printPrefixOpcodeEnum(opcodes)

// MARK: - Modify cpu instructions

//file = frameworkDir.appendingPathComponent("Cpu+Instructions.swift")
//let instructionsFileContent = try! String(contentsOf: file, encoding: .utf8)
//freopen(file.path, "w", stdout)
//modifyInstructions(opcodes, instructionsFileContent)
// file = frameworkDir.appendingPathComponent("Cpu+Instructions.swift")
// let instructionsFileContent = try! String(contentsOf: file, encoding: .utf8)
// freopen(file.path, "w", stdout)
// modifyInstructions(opcodes, instructionsFileContent)
3 changes: 3 additions & 0 deletions Sources/GameBoyKit/Cartridge/CartridgeMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// swiftformat:disable consecutiveSpaces
// swiftformat:disable redundantSelf

internal enum CartridgeMap {
internal static let interruptVectors: ClosedRange<UInt16> = 0x0000...0x00ff
internal static let entryPoint: ClosedRange<UInt16> = 0x0100...0x0103
Expand Down
6 changes: 3 additions & 3 deletions Sources/GameBoyKit/Cartridge/Header/CartridgeHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ private func isChecksumValid(_ data: Data) -> ChecksumResult {
var hl: UInt16 = CartridgeMap.headerChecksumRange.start

var b: UInt8 = 0x19 // bootrom: 0x00f1
var a: UInt8 = b // bootrom: 0x00f3
var a: UInt8 = b // bootrom: 0x00f3

while b > 0 {
a &+= data[hl] // bootrom: 0x00f4
hl += 1 // bootrom: 0x00f5
b -= 1 // bootrom: 0x00f6
hl += 1 // bootrom: 0x00f5
b -= 1 // bootrom: 0x00f6
}
a &+= data[hl] // bootrom: 0x00f9

Expand Down
1 change: 1 addition & 0 deletions Sources/GameBoyKit/Cpu/Cpu+Instructions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// swiftlint:disable file_length
// swiftformat:disable consecutiveSpaces

// This file is massive, but we need it this way so we can easier Cmd+F.
// Source:
Expand Down
12 changes: 6 additions & 6 deletions Sources/GameBoyKit/Cpu/Cpu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public final class Cpu {

private func getInterruptHandlingRoutine(_ type: Interrupts.Kind) -> UInt16 {
switch type {
case .vBlank: return 0x40
case .vBlank: return 0x40
case .lcdStat: return 0x48
case .timer: return 0x50
case .serial: return 0x58
case .joypad: return 0x60
case .timer: return 0x50
case .serial: return 0x58
case .joypad: return 0x60
}
}

Expand All @@ -119,7 +119,7 @@ public final class Cpu {

/// Next 16 bits after pc
internal var next16: UInt16 {
let low = UInt16(self.read(self.pc + 1))
let low = UInt16(self.read(self.pc + 1))
let high = UInt16(self.read(self.pc + 2))
return (high << 8) | low
}
Expand All @@ -143,7 +143,7 @@ public final class Cpu {
}

internal func pop16() -> UInt16 {
let low = UInt16(self.pop8())
let low = UInt16(self.pop8())
let high = UInt16(self.pop8())
return (high << 8) | low
}
Expand Down
Loading

0 comments on commit f1db357

Please sign in to comment.