problem with compilation

M

Ma Sz

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
 
S

Sam Duncan

[Note: parts of this message were removed to make it a legal post.]

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

Ma Sz

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

Eric Christopherson

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

Move output = '' to before the while loop.
 
M

Ma Sz

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
 
E

Eric Christopherson

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

It appears that you're dealing with an esoteric language called
Hexagrid: http://www.esolangs.org/wiki/Hexagrid

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.
 
G

Gunther Diemant

[Note: parts of this message were removed to make it a legal post.]

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

Ma Sz

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top