olavo_calculator 0.1.1 → 0.1.2

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: c4ca2d1111c63e314d0bd9770d27d09d5594cdd9
4
- data.tar.gz: d77214aecffde3b00a761bb9242a19107aacb9fc
3
+ metadata.gz: 7e733ad4d0f46a7173286053f503bcac62a58169
4
+ data.tar.gz: 5eff90191846361fcfe74aac041e6ed17df18b69
5
5
  SHA512:
6
- metadata.gz: ba877e24f22e89f9c6a00c4c850f2fbd5c77597ac064c89a964ae622db3e0cf74e940c002459083ab4a7b30055f9c10101a7101799a8b6616768c1b7756e04bc
7
- data.tar.gz: dfa4f7ebdeb255b6abc16d34040813c11c00ed3e86f2b6c4c6fdb906962d3680a91b7b46da511234d1fa00ae0645c4cafff70f702982659646dd40740640d69a
6
+ metadata.gz: efa3c3c25254100aafacefce67366fe122ea8912f56a4b2eb901fe2bd50fb69802eeeae2c106130aef6970dc264dfe6977d00c2f748ce7411b766e711177bfbd
7
+ data.tar.gz: 4877f84c3daa2951157469baf2d10ebb5e152f143bf7ab242555bc87964c1e8c38778601ff69c568f37e67eab4498cb7bc186f4de19ed71bc360d506b877836d
@@ -6,6 +6,8 @@ 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
10
+ puts e.message
9
11
  end
10
12
 
11
13
  default_task :solve
@@ -13,55 +13,58 @@ class OlavoCalculator::Lexer
13
13
  :'/' => :div
14
14
  }
15
15
 
16
- def self.tokenize string
17
- tokens = []
16
+ def tokenize string
17
+ @tokens = []
18
+ @string = string
19
+
18
20
  last_token = nil
19
21
  i = 0
20
22
 
21
- while i < string.length do
22
- i = detect_token(string, i, tokens)
23
+ while i < @string.length do
24
+ i = detect_token i
23
25
  end
24
26
 
25
- tokens
27
+ @tokens
26
28
  end
27
29
 
28
- def self.detect_token string, index, tokens
29
- current = string[index]
30
+ private
31
+ def detect_token index
32
+ current = @string[index]
30
33
 
31
- if current =~ /\s/
32
- return index + 1
33
- end
34
+ return (index + 1) if current =~ /\s/
34
35
 
35
- if current =~ /\d/ || (current =~ /-/ && (index + 1) < string.length && string[index + 1] =~ /\d/)
36
- return detect_number string, index, tokens
37
- end
36
+ return detect_number(index) if current =~ /\d/ || negative?(index)
38
37
 
39
38
  if TOKENS.keys.include? current.to_sym
40
- tokens << OlavoCalculator::Token.new(TOKENS[current.to_sym], current.to_sym)
39
+ @tokens << OlavoCalculator::Token.new(TOKENS[current.to_sym], current.to_sym)
41
40
  return index + 1
42
41
  end
43
42
 
44
- raise "Syntax Error"
43
+ raise OlavoException, "Syntax Error"
45
44
  end
46
45
 
47
- def self.detect_number string, index, tokens
48
- number = string[index]
46
+ def negative?(index)
47
+ @string[index] =~ /-/ && (index + 1) < @string.length && @string[index + 1] =~ /\d/
48
+ end
49
+
50
+ def detect_number index
51
+ number = @string[index]
49
52
 
50
- index = get_number number, string, index + 1
53
+ index = get_number number, index + 1
51
54
 
52
- if index + 1 < string.length && string[index] =~ /\./ && string[index + 1] =~ /\d/
55
+ if index + 1 < @string.length && @string[index] =~ /\./ && @string[index + 1] =~ /\d/
53
56
  number << '.'
54
- index = get_number number, string, index + 1
57
+ index = get_number number, index + 1
55
58
  end
56
59
 
57
- tokens << OlavoCalculator::Token.new(:number, number.to_f)
60
+ @tokens << OlavoCalculator::Token.new(:number, number.to_f)
58
61
 
59
62
  index
60
63
  end
61
64
 
62
- def self.get_number number, string, index
63
- while index < string.length && string[index] =~ /\d/ do
64
- number << string[index]
65
+ def get_number number, index
66
+ while index < @string.length && @string[index] =~ /\d/ do
67
+ number << @string[index]
65
68
  index += 1
66
69
  end
67
70
 
@@ -0,0 +1,2 @@
1
+ class OlavoException < Exception
2
+ end
@@ -1,3 +1,3 @@
1
1
  module OlavoCalculator
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,11 +1,12 @@
1
1
  require "olavo_calculator/version"
2
+ require "olavo_calculator/olavo_exception"
2
3
  require "olavo_calculator/token"
3
4
  require "olavo_calculator/lexer"
4
5
  require "olavo_calculator/parser"
5
6
 
6
7
  module OlavoCalculator
7
8
  def self.solve string
8
- token_array = Lexer.tokenize string
9
+ token_array = Lexer.new.tokenize string
9
10
  Parser.parse token_array
10
11
  end
11
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olavo_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Cardoso Dias
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-18 00:00:00.000000000 Z
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -86,6 +86,7 @@ files:
86
86
  - lib/olavo_calculator.rb
87
87
  - lib/olavo_calculator/cli.rb
88
88
  - lib/olavo_calculator/lexer.rb
89
+ - lib/olavo_calculator/olavo_exception.rb
89
90
  - lib/olavo_calculator/parser.rb
90
91
  - lib/olavo_calculator/token.rb
91
92
  - lib/olavo_calculator/version.rb