munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.43k stars 1.01k forks source link

Chapter 4 End State #1071

Closed philip-sparks closed 1 year ago

philip-sparks commented 1 year ago

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?

> "Hello World"
lox.Token@2a139a55
lox.Token@15db9742
> Hello World
lox.Token@6d06d69c
lox.Token@7852e922
lox.Token@4e25154f
> 1.2 + 2.3
lox.Token@55f96302
lox.Token@3d4eac69
lox.Token@42a57993
lox.Token@75b84c92
> 1 + 2
lox.Token@6bc7c054
lox.Token@232204a1
lox.Token@4aa298b7
lox.Token@7d4991ad

Thanks for reading!

chrisjbreisch commented 1 year 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
>
chrisjbreisch commented 1 year ago

I'm guessing you're missing the toString() bit from Token.

public String toString() {
    return type + " " + lexeme + " " + literal;
  }
philip-sparks commented 1 year ago

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
chrisjbreisch commented 1 year ago

You have toSting(), not toString(). toString() is called from System.out.println().

philip-sparks commented 1 year ago

Got it! So sorry it was such a simple mistake.

chrisjbreisch commented 1 year ago

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.