Closed philip-sparks closed 2 years ago
It's been a while since I did this chapter, but I did keep all my code by chapters. I don't think your output is correct, but I can't swear to it without re-reading the chapter.
Here's what I have:
> "Hello World"
STRING "Hello World" Hello World
EOF
> Hello World
IDENTIFIER Hello
IDENTIFIER World
EOF
> 1.2 + 2.3
NUMBER 1.2 1.2
PLUS +
NUMBER 2.3 2.3
EOF
> 1 + 2
NUMBER 1 1
PLUS +
NUMBER 2 2
EOF
>
I'm guessing you're missing the toString() bit from Token.
public String toString() {
return type + " " + lexeme + " " + literal;
}
I have toString() in the Token.java file (see below). I notice that Scanner.java and Lox.java never call toString() so far. Is that correct?
package lox;
class Token {
final TokenType type;
final String lexeme;
final Object literal;
final int line;
Token(TokenType type, String lexeme, Object literal, int line){
this.type = type;
this.lexeme = lexeme;
this.literal = literal;
this.line = line;
}
public String toSting(){
return type + " " + lexeme + " " + literal;
}
}
This is the current output after it compiles:
> cd "/Users/psparks/Documents/Crafting Interpreters" ; /usr/bin/env /usr/local/Cellar/openjdk@8/1.8.0+302/libexec/openjdk.jdk/Contents/Home/bin/java -cp "/Users/psparks/Library/Application Support/Code/User/workspaceStorage/4d9d740aa19498d0082437b9c7089884/redhat.java/jdt_ws/Crafting Interpreters_b100f141/bin" lox.Lox
lox.Token@7ea987ac
lox.Token@12a3a380
lox.Token@29453f44
lox.Token@5cad8086
lox.Token@6e0be858
lox.Token@61bbe9ba
lox.Token@610455d6
lox.Token@511d50c0
lox.Token@60e53b93
lox.Token@5e2de80c
lox.Token@1d44bcfa
You have toSting()
, not toString()
. toString()
is called from System.out.println()
.
Got it! So sorry it was such a simple mistake.
No worries. Lots of times it just takes a second set of eyes. Good luck and enjoy the rest of the book! I know I did.
And with that, we now have a complete scanner for the entire Lox lexical grammar. Fire up the REPL and type in some valid and invalid code. Does it produce the tokens you expect? - End of Chapter 4.7
What is the supposed end state for this chapter? My examples appear to have tokens with memory addresses. Is that what we should have at this stage?
Thanks for reading!