rpn-calculator 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3de84cffbeb26378a73daf456bdf6a617e876d5
4
- data.tar.gz: '08c6d4901a8a7272cd2bc3a68711a795659b8a61'
3
+ metadata.gz: 67b5ea07408d666e0b0b1f0e564b989773f7e095
4
+ data.tar.gz: 37623c7573e54025ff442b97c7c024b6ea09a9e7
5
5
  SHA512:
6
- metadata.gz: 3d0627889fa8c2ad58afc8483a4f78a9fa9655fe077eed1c647050e338a6dca558542eb03d7b4fb09319c7e2ca0fea9b6aad0a8bb5ca538d21ac9f4483f9c4b9
7
- data.tar.gz: 4f563e0a74d5e0044885b02166107b381e00bac97738f5709345aa472e24e0903cf648b519a5159fe653e6853589412bd9d76c4e4c60994b5ec35a23478be919
6
+ metadata.gz: e84986323f02c63a8dfa0cac0a144ff0ddb4916a83489a8f91088d2e0dbf24bd3d857749f1d8338bb9e971526f6c8b351b4f36851b855cee9c02a08b6ec43547
7
+ data.tar.gz: eacb1e523c7b9d4d98c4468ae8982ba251f19409c0a9256859be731ede0a108fba549c3b22e44cfea3cf36eb2edb0643993827ab6032e9c293b1099220721d08
@@ -9,6 +9,7 @@ module RPNCalculator
9
9
  def start
10
10
  while (input = io_interface.read_input)
11
11
  processor_result = operation_processor.process(input_stack, input)
12
+
12
13
  if processor_result.valid?
13
14
  print_result_array(processor_result.result)
14
15
  @input_stack = processor_result.result
@@ -1,8 +1,12 @@
1
1
  module RPNCalculator
2
2
  module Operation
3
3
  class Base
4
+ def self.operate(operands)
5
+ new(operands).result
6
+ end
7
+
4
8
  def initialize(operands)
5
- @operands = operands
9
+ @operands = operands.compact
6
10
  end
7
11
 
8
12
  def result
@@ -24,6 +28,10 @@ module RPNCalculator
24
28
  def invalid_operation_result(operation)
25
29
  Result::Operation.new(operation, [], operands)
26
30
  end
31
+
32
+ def operation_string
33
+ raise 'must implement in subclass'
34
+ end
27
35
  end
28
36
  end
29
37
  end
@@ -7,12 +7,14 @@ module RPNCalculator
7
7
  @operation_symbols = @operation_classes.keys
8
8
  end
9
9
 
10
+ # Refator needed, process should receive parsed input
10
11
  def process(previous_operations, input)
11
12
  validator_result = input_validator.validate(input)
12
13
  return Result::Processor.new([], validator_result.error) unless validator_result.valid?
13
14
 
14
15
  parser_result = input_parser.parse(input)
15
16
  return Result::Processor.new([], parser_result.error) unless parser_result.valid?
17
+
16
18
  process_operations(previous_operations + parser_result.parsed_elements)
17
19
  end
18
20
 
@@ -21,19 +23,18 @@ module RPNCalculator
21
23
  attr_reader :input_validator, :input_parser, :operation_classes, :operation_symbols
22
24
 
23
25
  def process_operations(operations)
24
- operation_stack = []
25
- while operations.size > 0
26
- element = operations.shift
26
+ result_stack = operations.inject([]) do |stack, element|
27
27
  if operation_symbols.include?(element)
28
- result = operation_classes.fetch(element).new(operation_stack).result
28
+ result = operation_classes.fetch(element)
29
+ .operate([stack.pop, stack.pop].reverse)
29
30
  return invalid_processor_result(result) unless result.valid?
30
-
31
- operation_stack = [result.result]
31
+ stack.push(result.result)
32
32
  else
33
- operation_stack.push(element)
33
+ stack.push(element)
34
34
  end
35
+ stack
35
36
  end
36
- Result::Processor.new(operation_stack)
37
+ Result::Processor.new(result_stack)
37
38
  end
38
39
 
39
40
  def invalid_processor_result(result)
@@ -1,3 +1,3 @@
1
1
  module RPNCalculator
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpn-calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Celi