Problem with compilation

I'm totally newbie at Ruby. What is wrong whit this code

class Hexagrid

    @@grid = [%w{ 0 1 2 3 }, %w{ 4 5 6 7 }, %w{ 8 9 A B }, %w{ C D E F
}]

    def initialize
        @stacks = Hash.new
    end

    def parse( string )
        while string =~ /^\s*(\S+)\s*\{([^\}]+)\}\s*;?\s*(.*)/

            output = ''
            stack = @stacks[$1] ||= Hash.new
            data = stack[:data] ||= Array.new
            pointer = stack[:pointer] ||= [0,0]

            $2.split(//).each do |c|
                  if c == '>' then pointer[1] += 1
               elsif c == '<' then pointer[1] -= 1
               elsif c == 'v' then pointer[0] += 1
               elsif c == '^' then pointer[0] -= 1
               elsif c == '!' then pointer = [0,0]; data.clear
               elsif c == '+' then data.unshift
@@grid[pointer[0]][pointer[1]]
               elsif c == 'o' then output << data.join
               end
            end
            string = $3
        end
        return output.reverse
    end

    def english( string )
        return parse(string).scan(/.{2}/).collect{ |tuple| tuple.hex.chr
}.join
    end
end

hex = Hexagrid.new
while not $stdin.eof?
  puts hex.english(gets)
end

I've got D:\NetBeansProjects\InterpreterHexagrid\lib\main.rb:33:in
`parse': undefined method `reverse' for nil:NilClass (NoMethodError)
        from D:\NetBeansProjects\InterpreterHexagrid\lib\main.rb:37:in
`english'
        from D:\NetBeansProjects\InterpreterHexagrid\lib\main.rb:43

···

--
Posted via http://www.ruby-forum.com/.

The input string in parse has to match your regex before output exists, however the function will attempt to reverse and return output regardless.

···

On 11/03/11 11:49, Ma Sz wrote:

class Hexagrid

     @@grid = [%w{ 0 1 2 3 }, %w{ 4 5 6 7 }, %w{ 8 9 A B }, %w{ C D E F
}]

     def initialize
         @stacks = Hash.new
     end

     def parse( string )
         while string =~ /^\s*(\S+)\s*\{([^\}]+)\}\s*;?\s*(.*)/

             output = ''
             stack = @stacks[$1] ||= Hash.new
             data = stack[:data] ||= Array.new
             pointer = stack[:pointer] ||= [0,0]

             $2.split(//).each do|c|
                   if c == '>' then pointer[1] += 1
                elsif c == '<' then pointer[1] -= 1
                elsif c == 'v' then pointer[0] += 1
                elsif c == '^' then pointer[0] -= 1
                elsif c == '!' then pointer = [0,0]; data.clear
                elsif c == '+' then data.unshift
@@grid[pointer[0]][pointer[1]]
                elsif c == 'o' then output<< data.join
                end
             end
             string = $3
         end
         return output.reverse
     end

     def english( string )
         return parse(string).scan(/.{2}/).collect{|tuple| tuple.hex.chr
}.join
     end
end

hex = Hexagrid.new
while not $stdin.eof?
   puts hex.english(gets)
end

hmm ok but how to fix it? :slight_smile: I don't know ruby langauge, i need only
this interpreter to compile hexagrid file

···

--
Posted via http://www.ruby-forum.com/.

ok, the error disappeared but for input
1d+d*dddd**++d1d+d*d*1d+*111++-++d1d+dd**1-++dd111+++11-+<[o<]!
i should get Hello but the while loop never stop

···

--
Posted via http://www.ruby-forum.com/.

Ok, thanks for help :slight_smile: It didn't wokr only with Neatbeans. I siwtched to
Linux and run the script using console. It wokrs fine :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Move output = '' to before the while loop.

···

On Thu, Mar 10, 2011 at 5:16 PM, Ma Sz <magda.szlagor@gmail.com> wrote:

hmm ok but how to fix it? :slight_smile: I don't know ruby langauge, i need only
this interpreter to compile hexagrid file

It appears that you're dealing with an esoteric language called
Hexagrid: Hexagrid - Esolang

And the Ruby-based interpreter you're using is from:
http://www.esolangs.org/wiki/User:JayCampbell/hexagrid.rb

I used the "hello world" script given on the wiki page, and it worked
fine. Perhaps you have an error in your Hexagrid code. Did you compose
that code yourself, or did you get it from somewhere else?

In particular, and this is just from skimming the Hexagrid wiki page,
you can't do anything unless you define a stack, e.g. "stack{...};".
Also, I don't see any indication in the wiki page or the interpreter's
code that [ or ] have any meaning in the language, but you use them
near the end.

···

On Thu, Mar 10, 2011 at 5:30 PM, Ma Sz <magda.szlagor@gmail.com> wrote:

ok, the error disappeared but for input
1d+d*dddd**++d1d+d*d*1d+*111++-++d1d+dd**1-++dd111+++11-+<[o<]!
i should get Hello but the while loop never stop

Its more like the while loop never runs cause your input doesn't match the
regexp (thats the reason you get your first error).

···

2011/3/11 Ma Sz <magda.szlagor@gmail.com>

ok, the error disappeared but for input
1d+d*dddd**++d1d+d*d*1d+*111++-++d1d+dd**1-++dd111+++11-+<[o<]!
i should get Hello but the while loop never stop

--
Posted via http://www.ruby-forum.com/\.