You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Creates a new `Dice` object from the specified string in dice notation.
///
/// You cannot have a negative die **AS A RESULT** (`-d6`), a die with negative sides (`d-6`), or a die with 0 sides (`d0`). You cannot have an unreal modifier or use any operator except for addition and subtraction.
///
/// You can have `-d6`s in your string, so long as they cancel each other out so that the final result is at least `0d6`.
///
/// - Parameter str: The string to convert.
/// - Throws: An `Error.IllegalNumberOfSides` error when the number of sides is less than or equal to 0
}else{throwError.illegalString(string: str)} //Too many "d"s (e.g. 3d4d6)
}else{throwError.illegalString(string: str)} //non-numeric and not a d, so...
}
}
}
vartempDice:[Die:Int]=[:]
for(d, c)in dice {
if c <0{
throwError.illegalString(string: str)
}elseif c ==0{
continue
}
tempDice[tryDie(sides: d)]= c
}
self.dice = tempDice
self.modifier = mods.sum
}
//swiftlint:enable function_body_length
So just by looking at the code for the string parsing of dice expressions, I think it should be pretty clear that it’s not a great implementation. To be fair, it does pass my fairly comprehensive set of unit tests, but I still think that a) that’s probably due to luck; b) even if it works, it’s not maintainable.
I’d like to replace it with recursive-descent parsing (or really, any structured type of parsing, but I have experience with recursive-descent, and I think it would work with this scenario.
The text was updated successfully, but these errors were encountered:
DiceKit/Sources/DiceKit/Dice.swift
Lines 168 to 252 in d67f52f
So just by looking at the code for the string parsing of dice expressions, I think it should be pretty clear that it’s not a great implementation. To be fair, it does pass my fairly comprehensive set of unit tests, but I still think that a) that’s probably due to luck; b) even if it works, it’s not maintainable.
I’d like to replace it with recursive-descent parsing (or really, any structured type of parsing, but I have experience with recursive-descent, and I think it would work with this scenario.
The text was updated successfully, but these errors were encountered: