pass all tests
This commit is contained in:
parent
a4cfed2cc4
commit
ff0aae8ece
2 changed files with 13 additions and 4 deletions
|
@ -14,10 +14,17 @@ module Rpn
|
||||||
splitted_input = input.split(' ')
|
splitted_input = input.split(' ')
|
||||||
parsed_tree = []
|
parsed_tree = []
|
||||||
|
|
||||||
|
opened_programs = 0
|
||||||
|
closed_programs = 0
|
||||||
|
string_delimiters = 0
|
||||||
regrouping = false
|
regrouping = false
|
||||||
|
|
||||||
splitted_input.each do |elt|
|
splitted_input.each do |elt|
|
||||||
parsed_entry = { value: elt }
|
parsed_entry = { value: elt }
|
||||||
|
|
||||||
|
opened_programs += 1 if elt[0] == '«'
|
||||||
|
string_delimiters += 1 if elt[0] == '"'
|
||||||
|
|
||||||
if regrouping
|
if regrouping
|
||||||
parsed_entry = parsed_tree.pop
|
parsed_entry = parsed_tree.pop
|
||||||
|
|
||||||
|
@ -46,11 +53,10 @@ module Rpn
|
||||||
parsed_entry[:value] = "'#{parsed_entry[:value]}'" if parsed_entry[:value][0] != "'"
|
parsed_entry[:value] = "'#{parsed_entry[:value]}'" if parsed_entry[:value][0] != "'"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# parsed_entry[:value] = elt[1..] if [:program, :string, :name].include?( parsed_entry[:type] )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
regrouping = ( (parsed_entry[:type] == :string && elt.size == 1 && elt[-1] != '"') ||
|
regrouping = ( (parsed_entry[:type] == :string && string_delimiters % 2 != 0) ||
|
||||||
(parsed_entry[:type] == :program && elt[-1] != '»') )
|
(parsed_entry[:type] == :program && opened_programs > closed_programs ) )
|
||||||
|
|
||||||
if parsed_entry[:type] == :numeric
|
if parsed_entry[:type] == :numeric
|
||||||
i = parsed_entry[:value].to_i
|
i = parsed_entry[:value].to_i
|
||||||
|
@ -59,6 +65,9 @@ module Rpn
|
||||||
parsed_entry[:value] = i == f ? i : f
|
parsed_entry[:value] = i == f ? i : f
|
||||||
end
|
end
|
||||||
|
|
||||||
|
closed_programs += 1 if elt[-1] == '»'
|
||||||
|
string_delimiters += 1 if elt.length > 1 && elt[-1] == '"'
|
||||||
|
|
||||||
parsed_tree << parsed_entry
|
parsed_tree << parsed_entry
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ class TestParser < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_word
|
def test_word
|
||||||
result = Rpn::Parser.new.parse_input( 'dup' )
|
result = Rpn::Parser.new.parse_input( 'dup' )
|
||||||
assert_equal [{ value: 'dup', type: :word }], result
|
assert_equal [{ value: "'dup'", type: :name }], result
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_string
|
def test_string
|
||||||
|
|
Loading…
Reference in a new issue