olavo_calculator 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e733ad4d0f46a7173286053f503bcac62a58169
4
- data.tar.gz: 5eff90191846361fcfe74aac041e6ed17df18b69
3
+ metadata.gz: 8fb77dc3076d776b6cc55c77e0d8745a95cc6a3f
4
+ data.tar.gz: d08dd0d9685d831816f5a849bfa9a4b504146e17
5
5
  SHA512:
6
- metadata.gz: efa3c3c25254100aafacefce67366fe122ea8912f56a4b2eb901fe2bd50fb69802eeeae2c106130aef6970dc264dfe6977d00c2f748ce7411b766e711177bfbd
7
- data.tar.gz: 4877f84c3daa2951157469baf2d10ebb5e152f143bf7ab242555bc87964c1e8c38778601ff69c568f37e67eab4498cb7bc186f4de19ed71bc360d506b877836d
6
+ metadata.gz: c72c091c88690c077d5c0943c05a4faaafd5d2a2ed227f65ba6bce17ba4ef1843e3631a731736e39f45fe05bab7d9e7d885c18006102408701ff76faa711b79c
7
+ data.tar.gz: 1c29c7c667886857fc401ce2aee837513da77390b302871f370f0090ba12c0dd5e19ce351727b911a82a1f3e8f2d054cf43882744e32b8d5b3c0e155e8aff6a1
@@ -7,6 +7,6 @@ require "olavo_calculator/parser"
7
7
  module OlavoCalculator
8
8
  def self.solve string
9
9
  token_array = Lexer.new.tokenize string
10
- Parser.parse token_array
10
+ Parser.new.parse token_array
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ module OlavoCalculator
6
6
  desc("solve EXPRESSION", "Solves the given EXPRESSION and returns the result")
7
7
  def solve expression
8
8
  puts OlavoCalculator.solve expression
9
- rescue Exception => e
9
+ rescue OlavoException => e
10
10
  puts e.message
11
11
  end
12
12
 
@@ -6,69 +6,71 @@ class OlavoCalculator::Parser
6
6
  :open_key => :close_key
7
7
  }
8
8
 
9
- def self.parse token_array
10
- detect_expression token_array
9
+ def parse token_array
10
+ @token_array = token_array
11
+ detect_expression
11
12
  end
12
13
 
13
14
  # expressões podem ser da forma: priority (PLUS|MINUS expr)*, term
14
- def self.detect_expression token_array
15
- raise "Unexpected Ending" if token_array.empty?
16
- value = detect_priority token_array
15
+ private
16
+ def detect_expression
17
+ raise OlavoException, "Unexpected Ending" if @token_array.empty?
18
+ value = detect_priority
17
19
 
18
- return value if token_array.empty?
20
+ return value if @token_array.empty?
19
21
 
20
- next_token = token_array.shift
22
+ next_token = @token_array.shift
21
23
 
22
24
  while next_token && [:plus, :minus].include?(next_token.type) do
23
- value += detect_term(token_array) if next_token.type == :plus
24
- value -= detect_term(token_array) if next_token.type == :minus
25
- next_token = token_array.shift
25
+ value += detect_term if next_token.type == :plus
26
+ value -= detect_term if next_token.type == :minus
27
+ next_token = @token_array.shift
26
28
  end
27
29
 
28
- token_array.unshift next_token
29
- token_array.unshift OlavoCalculator::Token.new(:number, value)
30
+ @token_array.unshift next_token
31
+ @token_array.unshift OlavoCalculator::Token.new(:number, value)
30
32
 
31
- detect_term token_array
33
+ detect_term
32
34
  end
33
35
 
34
36
  # prioridades podem ser da forma: (expr), [expr], {expr}, term
35
- def self.detect_priority token_array
36
- raise "Unexpected Ending" if token_array.empty?
37
+ def detect_priority
38
+ raise OlavoException, "Unexpected Ending" if @token_array.empty?
37
39
 
38
- if PRIORITY_SEMANTICS.key? token_array[0].type
39
- priority = token_array.shift
40
- value = detect_expression token_array
41
- raise "Semantic Error" unless token_array.shift.type == PRIORITY_SEMANTICS[priority.type]
40
+ if PRIORITY_SEMANTICS.key? @token_array[0].type
41
+ priority = @token_array.shift
42
+ value = detect_expression
43
+ raise OlavoException, "Semantic Error" unless @token_array.shift.type == PRIORITY_SEMANTICS[priority.type]
42
44
  return value
43
45
  end
44
46
 
45
- detect_term token_array
47
+ detect_term
46
48
  end
47
49
 
48
50
  # termos podem ser da forma: factor ((MULT|DIV) factor)*
49
- def self.detect_term token_array
50
- raise "Unexpected Ending" if token_array.empty?
51
- value = detect_factor token_array
51
+ def detect_term
52
+ raise OlavoException, "Unexpected Ending" if @token_array.empty?
53
+ value = detect_factor
52
54
 
53
- return value if token_array.empty?
55
+ return value if @token_array.empty?
54
56
 
55
- next_token = token_array.shift
57
+ next_token = @token_array.shift
56
58
 
57
59
  while next_token && [:mult, :div].include?(next_token.type) do
58
- value *= detect_factor(token_array) if next_token.type == :mult
59
- value /= detect_factor(token_array) if next_token.type == :div
60
- next_token = token_array.shift
60
+ value *= detect_factor if next_token.type == :mult
61
+ value /= detect_factor if next_token.type == :div
62
+ next_token = @token_array.shift
61
63
  end
62
64
 
63
- token_array.unshift next_token unless next_token.nil?
65
+ @token_array.unshift next_token unless next_token.nil?
64
66
 
65
67
  value
66
68
  end
67
69
 
68
70
  # fatores podem ser da forma: NUMBER, priority
69
- def self.detect_factor token_array
70
- raise "Unexpected Ending" if token_array.empty?
71
- return token_array.shift.value if token_array[0].type == :number
72
- detect_priority token_array
71
+ def detect_factor
72
+ raise OlavoException, "Unexpected Ending" if @token_array.empty?
73
+ return @token_array.shift.value if @token_array[0].type == :number
74
+ detect_priority
73
75
  end
74
76
  end
@@ -1,3 +1,3 @@
1
1
  module OlavoCalculator
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olavo_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Cardoso Dias