Interpreter challenge (C++): tester somehow fails to execute correctly else statement tests (stage ST5)

Hello, so I decided to tackle the interpreter challenge in C++, and for some reason, it fails at the second test (the random integer age one). Now, I usually take the test scripts and test them locally myself to see if I didn’t flub something up, but this time, while the tester fails, the local interpreter did not actually throw a parsing error and went through the script as the tester expected (tester got a ‘Expected ; after expression’ message for some reason. I cannot debug myself as this is a problem only reproducible on the testers. If you can access my code, then perhaps you can try compiling it on your end and see whether it works? (To get as close as possible to the tester’s condition, I elected to build and debug using WSL.)

In desperation, I tried adding some hacky checks for the tester so it can proceed anyway, so don’t mind the macro in main.cpp. (I managed to get past that step of the challenge after setting a hacky patch up, but I will remove said patch once the challenge is finished.)

Here is the test Lox script:

var age = 30;  // The integer actually changes
if (age > 18) print "adult"; else print "child";

Hey @fortwoone, could you upload to GitHub a version of your code without the hack and share the link? It will be much easier to debug if I can run it directly.

Okay, I’ll do it as soon as possible! I don’t actually know how to do this, though, do I just share from what I currently have?

1 Like

I do have access to your current code that includes the hack. It would be super helpful if you could also share a version that works locally without the hack.

Well, you can comment out the define macro. That’s all that needs to be done for it to work. Do you want me to make a commit for this?

None of the patches will run if the CODECRAFTERS_PATCHES macro is not defined

var age = 31;
if (age > 18) print "adult"; else print "child";

OK, I commented out the CODECRAFTERS_PATCHES macro. The error seems to happen in get_expr_statement:

consume(TokenType::SEMICOLON, "Expected ';' after expression???");
check(token_type))

Inside check, I tried printing peek().get_token_type() and type, but couldn’t get it to compile.

    bool Parser::check(TokenType type){
        if (is_at_end()){
            return false;
        }
        return peek().get_token_type() == type;
    }

Any suggestions on how to inspect these values properly? @fortwoone

Hmm, weird. How did you print it? Did you use cout or printf?

I had trouble printing stuff, but here’s what I got so far:

Tokens have unexpected “adult child” at the beginning. Investigating how they got into tokens.

I see, thanks for taking a look for me!

Quick update: they came from within the comments:

@fortwoone Feel free to test locally using quoted strings inside comments:

// This program initializes age with a random integer and then prints "adult"
// if the age is greater than 18, otherwise it prints "child"
var age = 42;
if (age > 18) print "adult"; else print "child";

…ARGH, RIGHT. Well, oops. Will try fixing that.Turns out it’s not a bug from your end… sorry for disturbing

1 Like

The fix is actually pretty stupid in hindsight as well, apologies

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.