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 +4 -4
- data/lib/olavo_calculator/cli.rb +2 -0
- data/lib/olavo_calculator/lexer.rb +27 -24
- data/lib/olavo_calculator/olavo_exception.rb +2 -0
- data/lib/olavo_calculator/version.rb +1 -1
- data/lib/olavo_calculator.rb +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e733ad4d0f46a7173286053f503bcac62a58169
|
4
|
+
data.tar.gz: 5eff90191846361fcfe74aac041e6ed17df18b69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efa3c3c25254100aafacefce67366fe122ea8912f56a4b2eb901fe2bd50fb69802eeeae2c106130aef6970dc264dfe6977d00c2f748ce7411b766e711177bfbd
|
7
|
+
data.tar.gz: 4877f84c3daa2951157469baf2d10ebb5e152f143bf7ab242555bc87964c1e8c38778601ff69c568f37e67eab4498cb7bc186f4de19ed71bc360d506b877836d
|
data/lib/olavo_calculator/cli.rb
CHANGED
@@ -13,55 +13,58 @@ class OlavoCalculator::Lexer
|
|
13
13
|
:'/' => :div
|
14
14
|
}
|
15
15
|
|
16
|
-
def
|
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
|
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
|
-
|
29
|
-
|
30
|
+
private
|
31
|
+
def detect_token index
|
32
|
+
current = @string[index]
|
30
33
|
|
31
|
-
|
32
|
-
return index + 1
|
33
|
-
end
|
34
|
+
return (index + 1) if current =~ /\s/
|
34
35
|
|
35
|
-
|
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
|
-
|
48
|
-
|
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,
|
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,
|
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
|
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
|
|
data/lib/olavo_calculator.rb
CHANGED
@@ -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.
|
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-
|
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
|