Ch. 20, p. 276 output

I’ve checked and double checked the code leading up to the output expected on p. 276. The code is functioning fine, and I can print individual elements of the output Token array. However, the print statement in Listing 20.13:

print("Lexer output: \(tokens)")

results in this output on my computer:

Evaluating: 10 + 3 + 5
Lexer output: [__lldb_expr_16.Token.number(10), __lldb_expr_16.Token.plus, __lldb_expr_16.Token.number(3), __lldb_expr_16.Token.plus, __lldb_expr_16.Token.number(5)]
Evaluating: 1 + 2 + abcdefg
An error occurred: invalidCharacter("a")

MacBook Pro, running OS 10.12.6, Xcode 9.0 (9A235), SDK 6.0.1+

You can get more readable output by making Token conform to the CustomStringConvertible protocol and implementing .description like this:

var description: String {
    switch self {
    case .Number(let value): return "\(value)"
    case .Plus: return "+"
    }
}
1 Like