Zomis / Brainduck

Brainfuck Interpreter in Java/Groovy, with a Groovy DSL
40 stars 1 forks source link

SpecialCommand.toString returns empty string if appending code #27

Open Zomis opened 8 years ago

Zomis commented 8 years ago

Current code:

    @Override
    public String toString() {
        'GroovyCommand'
    }

Desired code:

    @Override
    public String toString() {
        'GroovyCommand: ' + this.code
    }

However, when changing this it somehow returns an empty string.

But doing this prints the code correctly:

    @Override
    public String toString() {
        println this.code
        'GroovyCommand'
    }
skiwi2 commented 8 years ago

However, when changing this it somehow returns an empty string.

Do you mean that this.code is returned as empty, or that the full text of 'GroovyCommand: ' + this.code is returned as empty?

Zomis commented 8 years ago

@skiwi2 If I remember correctly, the full text of 'GroovyCommand: ' + this.code is returned as empty.

skiwi2 commented 8 years ago

To me this looks like your configuration has a caching issue, I get the following output when running Analyze on the by default provided BF code (only a part of the output is provided here):

Code instructions per command NONE: 3732 ADD: 270 WHILE: 39 NEXT: 100 PREVIOUS: 54 SUBTRACT: 76 END_WHILE: 39 GroovyCommand: lastLoop 'initialize100': 1 GroovyCommand: assert value == 100: 1 GroovyCommand: loop 'setupFizz': 1 GroovyCommand: loop 'setupBuzz': 1 GroovyCommand: nextLoop 'gotoStart': 1 GroovyCommand: loop 'main': 1 GroovyCommand: nextLoop 'find254': 1 GroovyCommand: loop 'notEnd': 1 GroovyCommand: nextLoop 'notZero': 1 GroovyCommand: nextLoop 'skipText': 1 GroovyCommand: nextLoop 'next254': 1 GroovyCommand: nextLoop 'matchFound': 1 GroovyCommand: nextLoop 'find255': 1 GroovyCommand: nextLoop 'setBoolean': 1 GroovyCommand: nextLoop 'find252': 1 GroovyCommand: nextLoop 'resetCountdown': 1 GroovyCommand: nextLoop 'printText': 1 WRITE: 5 GroovyCommand: nextLoop 'next254': 1 GroovyCommand: nextLoop 'search255_afterFizzBuzzes': 1 GroovyCommand: nextLoops 'printNumber': 1 net.zomis.brainf.model.groovy.GroovySupportConverter$1@562a4221: 1 Total: 4336

Rightmost memory accessed is 31

skiwi2 commented 8 years ago

I am not entirely sure how your Map<BrainfuckCommand, Integer> approach (in CommandCountAnalysis) is working though...

To me it looks like no hashcode and equals methods are provided in the BrainfuckCommand class, which makes them unsuitable to be used as keys in a Map.

Zomis commented 8 years ago

Okay, now it seems to work. No idea what caused it.

The CommandCountAnalysis is meant to count how many times each command is performed, totally unrelated to this issue. BrainfuckCommand is an interface BTW, not a class. And this SpecialCommand should not have hashCode and equals as two commands that have the same code should not be considered the same command for the CommandCountAnalysis. Using Object.hashCode and Object.equals does not make them unsuitable for use in a map. It's more when you have mutable objects, and put one into the map and retrieve another that it's unsuitable, or when you want a different equals check.

Zomis commented 8 years ago

This problem just re-appeared again when I did an analysis from command line. Apparently it is not fixed.