Ive got a token class, and a lexer class. I want to have a get_token_value method in the lexer class and I can't seem to get it. The Token and Lexer classes are below with only relevant methods.
This is the code used to try to get the values of all tokens in the @tokens array of the lexer
while lex.has_next
puts (lex.get_token_value) //error here # yeah, I'm a java port
lex.get_next
end
Interpeter says value is undefined. The @tokens array is filled properly with tokens by the lexer's tokenize method(not shown). What am I not understanding here? i've tried atrrib_reader as well to the same effect. When I do
puts lex.get_next
I get the to_s of the object, but that is not the desired behavior, just a kludge.
Class Token
def initialize( l, v)
@label = l
@value = v
end
def value
@value
end
def label
@label
end
def to_s
" #{label} #{value} "
end
end
class Lexer
def initialize
@tokens = Array.new
end
def get_token_value
@tokens[0].value
end
def get_token_label
@tokens[0].label
end
# consumes the current token.
def get_next
return @tokens.delete_at 0
end
def has_next
if @current < @tokens.size
return true
else
return false
end
end
end